Windows系统清理与优化脚本
一键清理Windows临时文件、回收站、浏览器缓存、系统更新缓存,释放磁盘空间并优化系统性能。
详细内容
# Windows系统清理与优化脚本
# 使用方法: 以管理员身份运行 PowerShell,执行 .\cleanup.ps1
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Windows系统清理与优化工具" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
$freedSpace = 0
# 1. 清理临时文件
Write-Host "【1/6】清理临时文件..." -ForegroundColor Yellow
$tempPaths = @(
$env:TEMP,
"C:\Windows\Temp",
"C:\Windows\Prefetch"
)
foreach ($path in $tempPaths) {
if (Test-Path $path) {
$before = (Get-ChildItem $path -Recurse -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum
Remove-Item "$path\*" -Recurse -Force -ErrorAction SilentlyContinue
$after = (Get-ChildItem $path -Recurse -ErrorAction SilentlyContinue | Measure-Object Length -Sum).Sum
$freedSpace += ($before - $after)
Write-Host " ✅ 已清理: $path"
}
}
# 2. 清空回收站
Write-Host ""
Write-Host "【2/6】清空回收站..." -ForegroundColor Yellow
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
Write-Host " ✅ 回收站已清空"
# 3. 清理系统更新缓存
Write-Host ""
Write-Host "【3/6】清理Windows更新缓存..." -ForegroundColor Yellow
Stop-Service wuauserv -Force -ErrorAction SilentlyContinue
$updatePath = "C:\Windows\SoftwareDistribution\Download"
if (Test-Path $updatePath) {
Remove-Item "$updatePath\*" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host " ✅ 更新缓存已清理"
}
Start-Service wuauserv -ErrorAction SilentlyContinue
# 4. 清理浏览器缓存
Write-Host ""
Write-Host "【4/6】清理浏览器缓存..." -ForegroundColor Yellow
$browserPaths = @(
"$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache",
"$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache",
"$env:APPDATA\Mozilla\Firefox\Profiles"
)
foreach ($path in $browserPaths) {
if (Test-Path $path) {
Remove-Item "$path\*" -Recurse -Force -ErrorAction SilentlyContinue
Write-Host " ✅ 已清理: $(Split-Path $path -Leaf)"
}
}
# 5. 清理DNS缓存
Write-Host ""
Write-Host "【5/6】刷新DNS缓存..." -ForegroundColor Yellow
ipconfig /flushdns | Out-Null
Write-Host " ✅ DNS缓存已刷新"
# 6. 系统优化设置
Write-Host ""
Write-Host "【6/6】应用系统优化设置..." -ForegroundColor Yellow
# 禁用启动项(示例,可根据需要调整)
Write-Host " ℹ️ 建议手动检查启动项: taskmgr -> 启动"
Write-Host ""
Write-Host "========================================" -ForegroundColor Green
Write-Host " 清理完成!" -ForegroundColor Green
Write-Host " 释放空间约: $([math]::Round($freedSpace/1MB, 2)) MB" -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Green
Write-Host ""
Write-Host "建议重启电脑使优化生效" -ForegroundColor Cyan