
历经数年迭代,Windows 系统维护功能已实现全面内置,用户无需依赖第三方应用即可保持操作系统清洁。尽管“存储感知”(Storage Sense)表现优异,但在某些细分领域仍存在清理盲区。通过结合自定义脚本,这些遗留问题可被有效解决。
第三方清理工具已失去存在价值
在 Windows 11 环境下,CCleaner 等第三方清理工具显得愈发多余。此类软件通常捆绑注册表清理、健康检查等功能,不仅实际益处寥寥,还因过度收集数据带来隐私风险。
事实上,残留注册表项体积极小,对系统性能毫无影响,清理后也不会显著增加可用空间。此外,这些工具的底层逻辑与“存储感知”类似,均是通过删除命令清除更新残留、缓存等文件。但它们常以后台服务形式运行,反而增加了额外的系统开销。
利用 AI 生成脚本填补清理空白
虽然“存储感知”可设置为每周自动运行,但仍无法覆盖所有垃圾文件区域。为此,可通过 AI 模型生成专用的 PowerShell 清理脚本,重点处理浏览器缓存、回收站及第三方应用临时文件,同时避开注册表操作。
以下是基于提示词生成的 PowerShell 脚本代码,具备试运行(Dry-run)、管理员权限请求、跳过占用文件及生成日志等功能:
#Requires -Version 5.1
[CmdletBinding()]
param([switch]$DryRun, [switch]$Schedule)
# Self-elevate
$admin = [Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $admin.IsInRole([Security.Principal.WindowsBuiltInRole].Administrator)) {
$argList = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" " +
(($PSBoundParameters.Keys | ForEach-Object { "-$_" }) -join ' ')
Start-Process powershell.exe -Verb RunAs -ArgumentList $argList
exit
}
# Register weekly task
if ($Schedule) {
$action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
$trigger = New-ScheduledTaskTrigger -Weekly -DaysOfWeek Sunday -At 10:00
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable
$princ = New-ScheduledTaskPrincipal -UserId "$env:USERDOMAIN\$env:USERNAME" -LogonType Interactive -RunLevel Highest
Register-ScheduledTask -TaskName 'WeeklyCleanup' -Action $action -Trigger $trigger -Settings $settings -Principal $princ -Force | Out-Null
Write-Host 'Scheduled: Sundays at 10:00.'
Read-Host 'Press Enter to close'
exit
}
# Cleanup targets
$local = $env:LOCALAPPDATA
$roam = $env:APPDATA
$chromium = 'Google\Chrome', 'Microsoft\Edge', 'BraveSoftware\Brave-Browser'
$targets = @(
$env:TEMP, "$env:WINDIR\Temp",
"$env:WINDIR\SoftwareDistribution\Download",
"$env:ProgramData\Microsoft\Windows\WER",
"$local\CrashDumps", "$local\Microsoft\Windows\INetCache",
"$local\Microsoft\Windows\Explorer\thumbcache_*.db",
"$local\Mozilla\Firefox\Profiles\*\cache2",
"$roam\discord\Cache", "$roam\discord\Code Cache",
"$roam\Slack\Cache", "$roam\Slack\Code Cache",
"$roam\Microsoft\Teams\Cache",
"$env:WINDIR\ServiceProfiles\NetworkService\AppData\Local\Microsoft\Windows\DeliveryOptimization\Cache",
"$env:WINDIR\Logs\CBS", "$env:WINDIR\Minidump",
"$env:WINDIR\LiveKernelReports", "$env:WINDIR\MEMORY.DMP",
"$local\npm-cache", "$local\pip\Cache", "$local\NuGet\v3-cache",
"$local\Spotify\Storage", "$roam\Adobe\Common\Media Cache Files"
)
foreach ($b in $chromium) {
foreach ($d in 'Cache', 'Code Cache', 'GPUCache') {
$targets += "$local\$b\User Data\*\$d"
}
$targets += "$local\$b\User Data\*\Service Worker\CacheStorage"
}
# Show estimated size of each target before cleanup
$targetReport = foreach ($path in $targets) {
$size = (Get-ChildItem -Path $path -File -Recurse -Force -ErrorAction SilentlyContinue |
Measure-Object Length -Sum).Sum
[pscustomobject]@{ Path = $path; GB = [math]::Round(([long]$size / 1GB), 2) }
}
$targetReport | Sort-Object GB -Descending | Format-Table -AutoSize
# Calculate size and optionally delete
function Clear-Targets {
[long]$bytes = 0
foreach ($path in $targets) {
foreach ($f in Get-ChildItem -Path $path -File -Recurse -Force -ErrorAction SilentlyContinue) {
if ($DryRun) { $bytes += $f.Length; continue }
try {
Remove-Item -LiteralPath $f.FullName -Force -ErrorAction Stop
$bytes += $f.Length
} catch { } # Skip locked files
}
}
return $bytes
}
# Run cleanup; restart only services that were running beforehand
$servicesToRestart = @()
try {
if (-not $DryRun) {
foreach ($name in 'wuauserv', 'bits', 'DoSvc') {
$service = Get-Service -Name $name -ErrorAction SilentlyContinue
if ($service -and $service.Status -eq 'Running') {
$servicesToRestart += $name
Stop-Service -Name $name -Force -ErrorAction SilentlyContinue
}
}
}
$freed = Clear-Targets
# Recycle Bin
$bin = (Get-ChildItem 'C:\$Recycle.Bin' -File -Recurse -Force -ErrorAction SilentlyContinue |
Measure-Object Length -Sum).Sum
if ($null -eq $bin) { $bin = 0 }
$freed += [long]$bin
if (-not $DryRun) { Clear-RecycleBin -DriveLetter C -Force -ErrorAction SilentlyContinue }
$mode = if ($DryRun) { 'DRY RUN (estimated)' } else { 'LIVE (attempted)' }
$line = '{0} | {1} | {2}: {3:N2} GB' -f
(Get-Date -Format 'yyyy-MM-dd HH:mm'), $(if ($DryRun) {'DRY'} else {'LIVE'}), $mode, ($freed / 1GB)
$logDir = [Environment]::GetFolderPath('MyDocuments')
if (-not (Test-Path -LiteralPath $logDir)) {
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
}
$log = Join-Path $logDir 'CleanupLog.txt'
Add-Content -Path $log -Value $line
Write-Host "`n$line" -ForegroundColor Green
Write-Host "Log: $log"
}
catch {
Write-Host "`nCleanup failed: $_" -ForegroundColor Red
}
finally {
foreach ($name in $servicesToRestart) {
Start-Service -Name $name -ErrorAction SilentlyContinue
}
Read-Host "`nCleanup finished. Press Enter to close"
}
脚本解析:精准覆盖清理盲区
该脚本旨在比“存储感知”更深入地释放存储空间,主要清理目标包括:
- 浏览器缓存:涵盖 Chrome、Edge、Firefox 等主流浏览器。
- 即时通讯与应用缓存:包括 Discord、Slack、Teams 及 Adobe 媒体缓存。
- 系统诊断文件:崩溃转储、Windows 错误报告及服务日志。
- 基础系统垃圾:回收站、临时文件夹及用户临时文件。
对于固态硬盘(SSD),清理这些缓存对性能提升有限,但能有效释放存储空间。用户可根据实际需求微调脚本,例如在机械硬盘上保留 Prefetch 文件以维持启动速度。
实测结果:额外释放 4.8GB 空间
将脚本保存为 .ps1 文件并在 PowerShell 中执行。测试环境中,一台总容量 256GB 的 C 盘在“存储感知”后台运行后仅剩 19.6GB 可用空间。执行该脚本后,额外清除了 4.8 GB 空间,效果显著优于多数第三方工具。
该方案的核心优势在于透明可控:用户可清晰知晓哪些文件夹被清理,脚本还会生成日志记录跳过的项目。整个脚本仅占用几 KB 空间,无后台驻留负担,且完全免费、无广告干扰。
保持 SSD 高效运行的建议
清理并非维持驱动器高速运行的唯一手段。建议始终保留约 20% 的存储空间空闲,以便 SSD 控制器进行垃圾回收和 SLC 缓存管理。
结合“存储感知”与此 PowerShell 脚本是良好的开端,但若追求极致效率,仍建议将个人数据和大型程序迁移至其他驱动器,以确保系统盘长期保持轻盈与快速。