# ========================================================= # MAX Windows Toolkit - 精简系统(安全版) # ========================================================= $Common = "C:\Setup\windows\lib\common.ps1" if (Test-Path $Common) { . $Common } else { # 回退函数:当 common.ps1 不存在时使用 function Write-Header { param([string]$Title) Clear-Host; Write-Host "==== $Title ====" -ForegroundColor Green } function Write-Section { param([string]$Text) Write-Host ""; Write-Host "-- $Text --" -ForegroundColor Magenta } function Write-OK { Write-Host " OK $($args[0])" -ForegroundColor Green } function Write-Warn { Write-Host " !! $($args[0])" -ForegroundColor Yellow } function Write-Err { Write-Host " XX $($args[0])" -ForegroundColor Red } function Write-Info { Write-Host " $($args[0])" -ForegroundColor White } function Write-Muted { Write-Host " $($args[0])" -ForegroundColor DarkGray } function Pause-Max { Write-Host ""; Read-Host "按 Enter 返回..." } function Write-Line { Write-Host "------------------------------" -ForegroundColor DarkGray } function Test-IsAdmin { try { $id = [Security.Principal.WindowsIdentity]::GetCurrent(); $p = New-Object Security.Principal.WindowsPrincipal($id); return $p.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) } catch { return $false } } function Show-AdminWarn { if (-not (Test-IsAdmin)) { Write-Warn "未以管理员身份运行,部分功能可能失败" } } } Show-AdminWarn # ───────────────────────────────────────── # 安全辅助函数 # ───────────────────────────────────────── function Ensure-RegistryKey { param([string]$Path) try { if (-not (Test-Path $Path)) { New-Item -Path $Path -Force -ErrorAction Stop | Out-Null } return $true } catch { Write-Err "创建注册表键失败: $Path" Write-Muted $_.Exception.Message return $false } } function Set-RegistryDwordSafe { param([string]$Path, [string]$Name, [int]$Value) try { if (Ensure-RegistryKey -Path $Path) { New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType DWord -Force -ErrorAction Stop | Out-Null Write-OK "注册表 DWORD: $Path\$Name = $Value" return $true } } catch { Write-Err "注册表 DWORD 失败: $Path\$Name" Write-Muted $_.Exception.Message } return $false } function Set-RegistryStringSafe { param([string]$Path, [string]$Name, [string]$Value) try { if (Ensure-RegistryKey -Path $Path) { New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType String -Force -ErrorAction Stop | Out-Null Write-OK "注册表 STRING: $Path\$Name = $Value" return $true } } catch { Write-Err "注册表 STRING 失败: $Path\$Name" Write-Muted $_.Exception.Message } return $false } function Remove-AppxPackageSafe { param([string]$PackagePattern) try { $packages = Get-AppxPackage -Name $PackagePattern -ErrorAction SilentlyContinue if (-not $packages) { Write-Muted "Appx 未找到: $PackagePattern" return } foreach ($pkg in $packages) { try { Remove-AppxPackage -Package $pkg.PackageFullName -ErrorAction Stop Write-Warn "已卸载 Appx: $($pkg.Name)" } catch { Write-Muted "无法卸载 Appx: $($pkg.Name) - $($_.Exception.Message)" } } } catch { Write-Muted "Appx 卸载跳过: $PackagePattern" } } function Remove-AppxProvisionedPackageSafe { param([string]$PackagePattern) try { $packages = Get-AppxProvisionedPackage -Online -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -like $PackagePattern } if (-not $packages) { Write-Muted "预配 Appx 未找到: $PackagePattern" return } foreach ($pkg in $packages) { try { Remove-AppxProvisionedPackage -Online -PackageName $pkg.PackageName -ErrorAction Stop | Out-Null Write-Warn "已卸载预配 Appx: $($pkg.DisplayName)" } catch { Write-Muted "无法卸载预配 Appx: $($pkg.DisplayName) - $($_.Exception.Message)" } } } catch { Write-Muted "预配 Appx 卸载跳过: $PackagePattern" } } function Disable-ServiceSafe { param([string]$ServiceName) try { $svc = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue if (-not $svc) { Write-Muted "服务未找到: $ServiceName" return } try { Stop-Service -Name $ServiceName -Force -ErrorAction SilentlyContinue } catch {} Set-Service -Name $ServiceName -StartupType Disabled -ErrorAction Stop Write-OK "服务已禁用: $ServiceName" } catch { Write-Muted "无法禁用服务: $ServiceName" } } function Stop-ProcessSafe { param([string]$ProcessName) try { Get-Process -Name $ProcessName -ErrorAction SilentlyContinue | Stop-Process -Force -ErrorAction SilentlyContinue Write-OK "已停止进程: $ProcessName" } catch { Write-Muted "无法停止进程: $ProcessName" } } function Restart-ExplorerSafe { Write-Info "正在重启资源管理器..." try { Stop-Process -Name explorer -Force -ErrorAction SilentlyContinue Start-Sleep -Seconds 2 Start-Process explorer.exe Write-OK "资源管理器已重启" } catch { Write-Warn "资源管理器重启失败" Write-Muted $_.Exception.Message } } # ───────────────────────────────────────── # 精简功能 # ───────────────────────────────────────── function Disable-ConsumerFeatures { Write-Section "禁用消费者功能和建议" # 系统级策略 Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent" -Name "DisableWindowsConsumerFeatures" -Value 1 | Out-Null Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent" -Name "DisableSoftLanding" -Value 1 | Out-Null Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent" -Name "DisableTailoredExperiencesWithDiagnosticData" -Value 1 | Out-Null # 当前用户建议 Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "ContentDeliveryAllowed" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "OemPreInstalledAppsEnabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "PreInstalledAppsEnabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "PreInstalledAppsEverEnabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SilentInstalledAppsEnabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SubscribedContent-338388Enabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SubscribedContent-338389Enabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SubscribedContent-353694Enabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SubscribedContent-353696Enabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SubscribedContent-353698Enabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\ContentDeliveryManager" -Name "SystemPaneSuggestionsEnabled" -Value 0 | Out-Null } function Disable-WidgetsChatAdsSearchHighlights { Write-Section "禁用小部件、聊天、广告和搜索高亮" $ExplorerAdvancedPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" $SearchSettingsPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\SearchSettings" $SearchPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Search" # Windows 11 小部件 / 任务视图 / 聊天 Set-RegistryDwordSafe -Path $ExplorerAdvancedPath -Name "TaskbarDa" -Value 0 | Out-Null Set-RegistryDwordSafe -Path $ExplorerAdvancedPath -Name "TaskbarMn" -Value 0 | Out-Null Set-RegistryDwordSafe -Path $ExplorerAdvancedPath -Name "ShowTaskViewButton" -Value 0 | Out-Null # 搜索高亮 Set-RegistryDwordSafe -Path $SearchSettingsPath -Name "IsDynamicSearchBoxEnabled" -Value 0 | Out-Null # Bing/Cortana 搜索 Set-RegistryDwordSafe -Path $SearchPath -Name "BingSearchEnabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path $SearchPath -Name "CortanaConsent" -Value 0 | Out-Null # 策略 Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Windows Feeds" -Name "EnableFeeds" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\Explorer" -Name "DisableSearchBoxSuggestions" -Value 1 | Out-Null } function Disable-TelemetryAndAdvertising { Write-Section "禁用遥测和广告 ID" Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\DataCollection" -Name "AllowTelemetry" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\AdvertisingInfo" -Name "Enabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Privacy" -Name "TailoredExperiencesWithDiagnosticDataEnabled" -Value 0 | Out-Null Disable-ServiceSafe "DiagTrack" Disable-ServiceSafe "dmwappushservice" } function Disable-TipsAndLockScreenContent { Write-Section "禁用提示和锁屏内容" Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\UserProfileEngagement" -Name "ScoobeSystemSettingEnabled" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced" -Name "Start_TrackProgs" -Value 0 | Out-Null Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent" -Name "DisableWindowsSpotlightFeatures" -Value 1 | Out-Null Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent" -Name "DisableSpotlightCollectionOnDesktop" -Value 1 | Out-Null Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent" -Name "DisableWindowsSpotlightOnActionCenter" -Value 1 | Out-Null Set-RegistryDwordSafe -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\CloudContent" -Name "DisableWindowsSpotlightOnSettings" -Value 1 | Out-Null } function Remove-BloatApps { Write-Section "卸载常见内置应用(无法卸载的将跳过)" $BloatAppPatterns = @( "Microsoft.3DBuilder", "Microsoft.BingFinance", "Microsoft.BingFoodAndDrink", "Microsoft.BingHealthAndFitness", "Microsoft.BingNews", "Microsoft.BingSports", "Microsoft.BingTravel", "Microsoft.BingWeather", "Microsoft.GetHelp", "Microsoft.Getstarted", "Microsoft.Microsoft3DViewer", "Microsoft.MicrosoftOfficeHub", "Microsoft.MicrosoftSolitaireCollection", "Microsoft.MicrosoftStickyNotes", "Microsoft.MixedReality.Portal", "Microsoft.Office.OneNote", "Microsoft.OneConnect", "Microsoft.People", "Microsoft.Print3D", "Microsoft.SkypeApp", "Microsoft.Todos", "Microsoft.Wallet", "Microsoft.WindowsAlarms", "Microsoft.WindowsFeedbackHub", "Microsoft.WindowsMaps", "Microsoft.WindowsSoundRecorder", "Microsoft.XboxApp", "Microsoft.XboxGamingOverlay", "Microsoft.XboxGameOverlay", "Microsoft.XboxSpeechToTextOverlay", "Microsoft.Xbox.TCUI", "Microsoft.YourPhone", "Microsoft.ZuneMusic", "Microsoft.ZuneVideo", "MicrosoftTeams", "Clipchamp.Clipchamp" ) foreach ($pattern in $BloatAppPatterns) { Remove-AppxPackageSafe -PackagePattern $pattern Remove-AppxProvisionedPackageSafe -PackagePattern $pattern } } function Keep-ImportantAppsNotice { Write-Section "默认保留的重要应用" Write-Info "以下应用不会被此脚本卸载:" Write-Muted "Microsoft Store" Write-Muted "Desktop App Installer / winget" Write-Muted "Windows 安全中心" Write-Muted "照片" Write-Muted "计算器" Write-Muted "记事本" Write-Muted "画图" Write-Muted "终端" } # ───────────────────────────────────────── # 主菜单 # ───────────────────────────────────────── function Run-SafeDebloat { Write-Header "精简系统 - 安全版" Write-Info "本脚本遵循安全规则:" Write-Muted "如果应用无法卸载,将自动跳过" Write-Muted "如果注册表项无法修改,将自动跳过" Write-Muted "脚本不会因单个失败项而停止" Write-Muted "Microsoft Store 和重要系统应用将被保留" Write-Line if (-not (Test-IsAdmin)) { Write-Warn "未以管理员身份运行,部分系统级设置可能失败" } $Confirm = Read-Host " 继续精简?输入 YES 确认" if ($Confirm -ne "YES") { Write-Warn "已取消" return } Keep-ImportantAppsNotice Disable-ConsumerFeatures Disable-WidgetsChatAdsSearchHighlights Disable-TelemetryAndAdvertising Disable-TipsAndLockScreenContent Remove-BloatApps Restart-ExplorerSafe Write-Line Write-OK "精简完成。部分跳过项在不同 Windows 版本上属正常现象" Write-Warn "建议重启 Windows" } :debloatLoop while ($true) { Write-Info "可用操作:" Write-Info "1. 运行安全精简" Write-Info "2. 禁用消费者功能和建议" Write-Info "3. 禁用小部件、聊天、广告和搜索高亮" Write-Info "4. 禁用遥测和广告 ID" Write-Info "5. 禁用提示和锁屏内容" Write-Info "6. 卸载常见内置应用" Write-Info "7. 重启资源管理器" Write-Info "0. 退出" Write-Line $Choice = Read-Host " 请选择" switch ($Choice) { "1" { Run-SafeDebloat Pause-Max } "2" { Disable-ConsumerFeatures Pause-Max } "3" { Disable-WidgetsChatAdsSearchHighlights Pause-Max } "4" { Disable-TelemetryAndAdvertising Pause-Max } "5" { Disable-TipsAndLockScreenContent Pause-Max } "6" { Remove-BloatApps Pause-Max } "7" { Restart-ExplorerSafe Pause-Max } "0" { break :debloatLoop } default { Write-Err "无效选项" Pause-Max } } }