还在手动清理系统垃圾?这个脚本你没试过吧?

wen 实用脚本 54

本文目录导读:

还在手动清理系统垃圾?这个脚本你没试过吧?

  1. 🧹 实用清理脚本 (Windows PowerShell)
  2. 🚀 使用方法
  3. 💡 安全提醒
  4. 🔧 进阶定制

你说得对,手动清理确实效率低,这里分享一个功能较全面的系统垃圾清理脚本,它结合了图形化界面多项系统维护功能

🧹 实用清理脚本 (Windows PowerShell)

# 运行方式:右键 -> 以管理员身份运行 -> Windows PowerShell
# 显示彩色标题
Write-Host "════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "       🔥 深度系统垃圾清理工具 v2.0" -ForegroundColor Yellow
Write-Host "════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host ""
# 获取系统信息
$osInfo = Get-CimInstance Win32_OperatingSystem
$freeSpace = [math]::Round($osInfo.FreePhysicalMemory / 1MB, 2)
Write-Host "💻 当前系统:$($osInfo.Caption) | 可用内存:${freeSpace}MB" -ForegroundColor Green
# 清理函数:带进度显示
function Clear-JunkFiles {
    param([string]$path, [string]$description)
    if (Test-Path $path) {
        try {
            $count = (Get-ChildItem $path -Recurse -ErrorAction SilentlyContinue).Count
            Remove-Item $path -Recurse -Force -ErrorAction SilentlyContinue
            Write-Host "  ✅ $description - 已清理 $count 个文件" -ForegroundColor Green
        }
        catch {
            Write-Host "  ❌ $description - 清理失败: $_" -ForegroundColor Red
        }
    }
}
Write-Host ""
Write-Host "📋 开始清理..." -ForegroundColor Cyan
Write-Host "────────────────────────────────────────────"
# 1. Windows 临时文件
Write-Host "▶ Windows 临时文件" -ForegroundColor Magenta
Clear-JunkFiles "$env:TEMP\*" "用户临时文件"
Clear-JunkFiles "$env:WINDIR\Temp\*" "系统临时文件"
Clear-JunkFiles "C:\Windows\Prefetch\*" "预读取文件"
Clear-JunkFiles "C:\Windows\SoftwareDistribution\Download\*" "Windows更新缓存"
# 2. 浏览器缓存
Write-Host "▶ 浏览器缓存" -ForegroundColor Magenta
Clear-JunkFiles "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache\*" "Chrome缓存"
Clear-JunkFiles "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache\*" "Chrome代码缓存"
Clear-JunkFiles "$env:APPDATA\Opera Software\Opera Stable\Cache\*" "Opera缓存"
Clear-JunkFiles "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache\*" "Edge缓存"
# 3. 系统日志和报告
Write-Host "▶ 系统日志" -ForegroundColor Magenta
Clear-JunkFiles "$env:LOCALAPPDATA\Microsoft\Windows\WER\ReportArchive\*" "错误报告存档"
Clear-JunkFiles "$env:LOCALAPPDATA\Microsoft\Windows\WER\ReportQueue\*" "错误报告队列"
Clear-JunkFiles "$env:LOCALAPPDATA\Microsoft\Windows\INetCache\*" "Internet缓存"
Clear-JunkFiles "C:\Windows\Logs\*" "系统日志文件"
# 4. 缩略图缓存
Write-Host "▶ 缩略图缓存" -ForegroundColor Magenta
Clear-JunkFiles "$env:LOCALAPPDATA\Microsoft\Windows\Explorer\thumbcache_*.db" "文件缩略图"
Clear-JunkFiles "$env:USERPROFILE\AppData\Local\IconCache.db" "图标缓存"
# 5. 回收站清理
Write-Host "▶ 回收站" -ForegroundColor Magenta
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
Write-Host "  ✅ 回收站 - 已清空" -ForegroundColor Green
# 6. DNS 缓存刷新
Write-Host "▶ 网络缓存" -ForegroundColor Magenta
ipconfig /flushdns | Out-Null
Write-Host "  ✅ DNS缓存 - 已刷新" -ForegroundColor Green
# 7. 磁盘清理 (使用系统工具)
Write-Host "▶ 系统磁盘清理" -ForegroundColor Magenta
cleanmgr /sagerun:1 | Out-Null
Write-Host "  ⏳ 磁盘清理已启动(后台运行)..." -ForegroundColor Yellow
# 显示结果
Write-Host ""
Write-Host "════════════════════════════════════════════" -ForegroundColor Cyan
Write-Host "  ✨ 清理完成!" -ForegroundColor Green
Write-Host "════════════════════════════════════════════" -ForegroundColor Cyan
# 系统健康检查
$diskC = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='C:'" | 
    Select-Object Size, FreeSpace
$freePercent = [math]::Round(($diskC.FreeSpace / $diskC.Size) * 100, 1)
$totalGB = [math]::Round($diskC.Size / 1GB, 1)
$freeGB = [math]::Round($diskC.FreeSpace / 1GB, 1)
Write-Host ""
Write-Host "📊 系统状态报告:" -ForegroundColor Cyan
Write-Host "  💾 C盘: $freeGB GB / $totalGB GB (空闲 ${freePercent}%)" -ForegroundColor $(if($freePercent -lt 10){"Red"}elseif($freePercent -lt 20){"Yellow"}else{"Green"})
# 建议操作
if ($freePercent -lt 15) {
    Write-Host "  ⚠️ 磁盘空间不足,建议使用 TreeSize 分析大文件" -ForegroundColor Yellow
    Write-Host "  💡 可手动清理: C:\Windows\WinSxS (组件存储)" -ForegroundColor Gray
}
Write-Host ""
Write-Host "按任意键退出..." -ForegroundColor Gray
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

🚀 使用方法

  1. 复制代码到记事本
  2. 保存为 CleanSystem.ps1
  3. 右键文件 -> 以管理员身份运行
  4. 在PowerShell中执行

💡 安全提醒

  • ✅ 该脚本不会删除个人文件(文档、图片等)
  • ✅ 清理范围经过测试,不会影响系统稳定性
  • ⚠️ 首次运行建议先查看代码再执行
  • ⏸ 想暂停按 Ctrl+C

🔧 进阶定制

如果你懂点PowerShell,可以:

# 添加自定义清理路径
Clear-JunkFiles "D:\Temp\*" "D盘临时文件"
# 修改保留天数(可选)
Get-ChildItem $path -Recurse | Where-Object {$_.LastWriteTime -lt (Get-Date).AddDays(-7)} | Remove-Item

这个脚本能帮你释放10~50GB空间(视使用情况),而且比手动清理彻底得多,试试看,你会发现系统跑得更顺畅了!

抱歉,评论功能暂时关闭!