=== Windows 11 极致精简与优化综合脚本 freevps.win 出版 ===
适用场景: AI开发
新增功能: 禁用Defender + 移除自启项 + 虚拟内存/系统保护/RDP优化
⚠️ 执行前请务必备份虚拟机快照!
Write-Host ">>> 开始执行系统极致精简与优化..." -ForegroundColor Yellow
Write-Host "=================================================" -ForegroundColor Gray
---------------------------------------------------------
Step 1: 卸载 33 个冗余 Appx 包 (已安装实体 + 预置镜像)
逻辑: 最先执行,避免后续禁用Defender导致Appx卸载验证失败
---------------------------------------------------------
Write-Host "[1/14] 正在卸载 33 个冗余应用包..." -ForegroundColor Cyan
$apps = @(
"Microsoft.BingNews", "Microsoft.BingWeather", "Microsoft.BingSearch",
"Microsoft.GetHelp", "Microsoft.Getstarted",
"Microsoft.MicrosoftSolitaireCollection", "Microsoft.MicrosoftOfficeHub",
"Microsoft.People", "Microsoft.PowerAutomateDesktop", "Microsoft.Todos",
"Microsoft.WindowsFeedbackHub", "Microsoft.WindowsMaps",
"Microsoft.ZuneMusic", "Microsoft.ZuneVideo",
"Microsoft.YourPhone", "Microsoft.WindowsAlarms",
"Microsoft.WindowsSoundRecorder", "Microsoft.549981C3F5F10",
"Microsoft.GamingApp", "Microsoft.XboxGamingOverlay",
"Microsoft.XboxSpeechToTextOverlay", "Microsoft.Xbox.TCUI",
"Microsoft.OutlookForWindows", "Microsoft.Windows.DevHome",
"Microsoft.Copilot", "MSTeams", "MicrosoftTeams",
"Clipchamp.Clipchamp", "MicrosoftCorporationII.QuickAssist",
"MicrosoftCorporationII.MicrosoftFamily",
"Microsoft.WindowsCamera", "Microsoft.MixedReality.Portal",
"Microsoft.Wallet", "Microsoft.Print3D"
)
foreach ($app in $apps) {
Write-Host " -> 移除: $app" -ForegroundColor DarkCyan
Get-AppxPackage -AllUsers -Name $app -ErrorAction SilentlyContinue | Remove-AppxPackage -AllUsers -ErrorAction SilentlyContinue
Get-AppxProvisionedPackage -Online | Where-Object DisplayName -eq $app | Remove-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue | Out-Null
}
Write-Host " ✅ 冗余应用包卸载完成" -ForegroundColor Green
---------------------------------------------------------
Step 2: 关闭系统休眠
逻辑: 立即释放 hiberfil.sys 空间,为后续操作提供缓冲
---------------------------------------------------------
Write-Host "`n[2/14] 正在关闭系统休眠..." -ForegroundColor Cyan
powercfg -h off
Write-Host " ✅ 休眠已关闭" -ForegroundColor Green
---------------------------------------------------------
Step 3: 清理临时文件、预读文件及 DoSvc 缓存
逻辑: 卸载后趁热清理残留,防止重启后被重新索引
---------------------------------------------------------
Write-Host "`n[3/14] 正在清理系统与用户临时文件及缓存..." -ForegroundColor Cyan
Remove-Item "$env:TEMP*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$env:SystemRoot\Temp*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "$env:SystemRoot\Prefetch*" -Recurse -Force -ErrorAction SilentlyContinue
$doCache = "$env:SystemRoot\ServiceProfiles\NetworkService\AppData\Local\Microsoft\Windows\DeliveryOptimization\Cache"
if (Test-Path $doCache) { Remove-Item "$doCache*" -Recurse -Force -ErrorAction SilentlyContinue }
Write-Host " ✅ 临时文件与缓存清理完成" -ForegroundColor Green
---------------------------------------------------------
Step 4: 禁用保留存储并执行 DISM 组件清理
逻辑: 耗时较长(5-15分钟),在文件清理后执行避免误判
---------------------------------------------------------
Write-Host "`n[4/14] 正在禁用保留存储并清理 WinSxS 冗余组件..." -ForegroundColor Cyan
Write-Host " ⏳ DISM 清理可能需要较长时间,请耐心等待..." -ForegroundColor Yellow
DISM.exe /online /Set-ReservedStorageState /State:Disabled /NoRestart | Out-Null
Dism /online /cleanup-image /StartComponentCleanup /NoRestart | Out-Null
Write-Host " ✅ 保留存储已禁用,组件清理完成" -ForegroundColor Green
---------------------------------------------------------
Step 5: 关闭虚拟内存 (所有驱动器设为无分页文件)
逻辑: 在DISM完成后执行,避免分页文件占用WinSxS清理空间。
注意: 若虚拟机内存<8GB或运行大型模型,建议保留自动管理。
---------------------------------------------------------
Write-Host "`n[5/14] 正在关闭虚拟内存 (设置无分页文件)..." -ForegroundColor Cyan
$cs = Get-WmiObject Win32_ComputerSystem
$cs.AutomaticManagedPagefile = $false
$cs.Put() | Out-Null
$pagefile = Get-WmiObject Win32_PageFileSetting
if ($pagefile) { $pagefile.Delete() | Out-Null }
Write-Host " ✅ 虚拟内存已关闭 (需重启生效)" -ForegroundColor Green
---------------------------------------------------------
Step 6: 优化系统失败恢复 & 启动超时
逻辑: 取消蓝屏自动重启便于调试;延长启动列表至8秒方便多系统选择
---------------------------------------------------------
Write-Host "`n[6/14] 正在优化系统失败恢复与启动超时..." -ForegroundColor Cyan
取消写入事件日志、取消自动重启
reg add "HKLM\SYSTEM\CurrentControlSet\Control\CrashControl" /v AutoReboot /t REG_DWORD /d 0 /f | Out-Null
reg add "HKLM\SYSTEM\CurrentControlSet\Control\CrashControl" /v LogEvent /t REG_DWORD /d 0 /f | Out-Null
设置启动操作系统列表显示时间为8秒
bcdedit /timeout 8 | Out-Null
Write-Host " ✅ 失败恢复与启动超时已优化" -ForegroundColor Green
---------------------------------------------------------
Step 7: 关闭系统保护并删除所有还原点
逻辑: 在虚拟内存关闭后执行,彻底释放磁盘空间
---------------------------------------------------------
Write-Host "`n[7/14] 正在关闭系统保护并删除还原点..." -ForegroundColor Cyan
Disable-ComputerRestore -Drive "C:\" -ErrorAction SilentlyContinue
vssadmin delete shadows /all /quiet | Out-Null
Write-Host " ✅ 系统保护已关闭,还原点已清除" -ForegroundColor Green
---------------------------------------------------------
Step 8: 配置远程桌面 (允许连接 + NLA验证)
逻辑: 在WU锁定前配置,避免被组策略覆盖
---------------------------------------------------------
Write-Host "`n[8/14] 正在配置远程桌面 (启用+NLA)..." -ForegroundColor Cyan
启用RDP
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections /t REG_DWORD /d 0 /f | Out-Null
仅允许网络级别身份验证(NLA)
reg add "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v UserAuthentication /t REG_DWORD /d 1 /f | Out-Null
防火墙放行RDP
Enable-NetFirewallRule -DisplayGroup "Remote Desktop" -ErrorAction SilentlyContinue
Write-Host " ✅ 远程桌面已启用 (NLA模式)" -ForegroundColor Green
---------------------------------------------------------
Step 9: 禁用 Delivery Optimization 服务
逻辑: 防止RDP配置后DoSvc再生成P2P缓存
---------------------------------------------------------
Write-Host "`n[9/14] 正在禁用 Delivery Optimization 服务..." -ForegroundColor Cyan
New-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\DoSvc" -Name "Start" -Value 4 -Type DWord -Force | Out-Null
Stop-Service DoSvc -Force -ErrorAction SilentlyContinue
Write-Host " ✅ DoSvc 已禁用" -ForegroundColor Green
---------------------------------------------------------
Step 10: 彻底禁用 Microsoft Defender 防病毒
逻辑: 所有文件/磁盘操作完成后执行,防止拦截删除
---------------------------------------------------------
Write-Host "`n[10/14] 正在彻底禁用 Microsoft Defender..." -ForegroundColor Cyan
New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender" -Force | Out-Null
New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender\Real-Time Protection" -Force | Out-Null
New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender\Signature Updates" -Force | Out-Null
New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender" -Name "DisableAntiSpyware" -Value 1 -Type DWord -Force | Out-Null
New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender\Real-Time Protection" -Name "DisableRealtimeMonitoring" -Value 1 -Type DWord -Force | Out-Null
New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows Defender\Signature Updates" -Name "DisableOnAccessProtection" -Value 1 -Type DWord -Force | Out-Null
Write-Host " ✅ Defender 三重策略已写入" -ForegroundColor Green
---------------------------------------------------------
Step 11: 移除 SecurityHealthSystray 和 OneDriveSetup 自启项
逻辑: 配合Defender禁用,隐藏托盘图标并阻止OneDrive弹窗
---------------------------------------------------------
Write-Host "`n[11/14] 正在移除安全中心与OneDrive自启项..." -ForegroundColor Cyan
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "SecurityHealth" -Force -ErrorAction SilentlyContinue
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "OneDriveSetup" -Force -ErrorAction SilentlyContinue
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Name "Block_SecurityHealth" -Value "" -Type String -Force | Out-Null
New-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce" -Name "Block_OneDriveSetup" -Value "" -Type String -Force | Out-Null
Write-Host " ✅ 自启项已移除并防复活" -ForegroundColor Green
---------------------------------------------------------
Step 12: 三重防护锁定 Windows Update (含WaaSMedicSvc)
逻辑: 所有联网/文件操作完成后才执行,防止阻断验证
---------------------------------------------------------
Write-Host "`n[12/14] 正在应用 WU 三重防护锁定..." -ForegroundColor Cyan
takeown /f "$env:SystemRoot\System32\WaaSMedicSvc.dll" /a >$null 2>&1
icacls "$env:SystemRoot\System32\WaaSMedicSvc.dll" /grant Administrators:F >$null 2>&1
Stop-Service WaaSMedicSvc -Force -ErrorAction SilentlyContinue
sc.exe config WaaSMedicSvc start= disabled | Out-Null
Get-ScheduledTask -TaskPath "\Microsoft\Windows\UpdateOrchestrator\" -ErrorAction SilentlyContinue | Disable-ScheduledTask -ErrorAction SilentlyContinue
Get-ScheduledTask -TaskPath "\Microsoft\Windows\WindowsUpdate\" -ErrorAction SilentlyContinue | Disable-ScheduledTask -ErrorAction SilentlyContinue
New-Item -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Force | Out-Null
New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" -Name "NoAutoUpdate" -Value 1 -Type DWord -Force | Out-Null
Write-Host " ✅ WU 三重防护已生效" -ForegroundColor Green
---------------------------------------------------------
Step 13: 禁用 Windows Update 主服务 (wuauserv)
逻辑: 最后一步保险,确保即使防护有遗漏也无法启动更新
---------------------------------------------------------
Write-Host "`n[13/14] 正在停止并禁用 wuauserv 主服务..." -ForegroundColor Cyan
Stop-Service wuauserv -Force -ErrorAction SilentlyContinue
sc.exe config wuauserv start= disabled | Out-Null
Write-Host " ✅ wuauserv 已禁用" -ForegroundColor Green
---------------------------------------------------------
完成
---------------------------------------------------------
Write-Host "`n=================================================" -ForegroundColor Gray
Write-Host "✅ 所有优化操作已完成!" -ForegroundColor Green
Write-Host "⚠️ 请务必立即重启虚拟机以释放休眠/分页文件空间并使服务变更生效。" -ForegroundColor Yellow
Write-Host "💡 建议重启后创建新快照作为纯净基准。" -ForegroundColor Cyan
Write-Host "`nScript by freevps.win" -ForegroundColor Magenta
---------------------------------------------------------
停顿等待:按空格键退出,其他键无效
---------------------------------------------------------
Write-Host "`n>>> 请检查以上执行结果,按 [空格键] 退出..." -ForegroundColor Yellow
do {
$key = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
} until ($key.VirtualKeyCode -eq 32)