Win10 冗余文件扫描 清理报告生成脚本

发布时间:2026/7/6 7:57:35
Win10 冗余文件扫描  清理报告生成脚本 # Win10 冗余文件扫描 清理报告生成脚本# 用法: powershell -ExecutionPolicy Bypass -File cleanup-redundant.ps1# 以管理员身份运行可获得更全面的扫描结果param([string[]]$ScanPaths ($env:USERPROFILE\Desktop, $env:USERPROFILE\Downloads, $env:USERPROFILE\Documents),[string]$ReportPath $env:USERPROFILE\Desktop\冗余文件清理报告.md,[switch]$CleanTemp $false,[switch]$CleanRecycleBin $false,[switch]$DryRun $true)$ErrorActionPreference SilentlyContinue$reportLines ()# ── 报告头部 ──$reportLines # Windows 10 冗余文件清理报告$reportLines $reportLines **扫描时间**: $(Get-Date -Format yyyy-MM-dd HH:mm:ss)$reportLines **扫描路径**: $($ScanPaths -join , )$reportLines $reportLines ---$reportLines # ════════════════════════════════════════# 1. 按 SHA256 查找重复文件# ════════════════════════════════════════$reportLines ## 1. 重复文件 (按 SHA256 哈希)$reportLines $reportLines 扫描较大的重复文件1MB保留一份、删除副本可释放空间。$reportLines $reportLines | 文件大小 | 数量 | 示例路径 |$reportLines |---------|------|---------|$duplicates {}$totalDuplicateSize 0foreach ($path in $ScanPaths) {if (-not (Test-Path $path)) { continue }Get-ChildItem -Path $path -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.Length -gt 1MB } | ForEach-Object {$hash (Get-FileHash -Path $_.FullName -Algorithm SHA256 -ErrorAction SilentlyContinue).Hashif ($hash) {if (-not $duplicates.ContainsKey($hash)) {$duplicates[$hash] ()}$duplicates[$hash] $_}}}$dupCount 0foreach ($hash in $duplicates.Keys) {$files $duplicates[$hash]if ($files.Count -gt 1) {$dupCount$size $files[0].Length$totalDuplicateSize $size * ($files.Count - 1)$reportLines | $([math]::Round($size/1MB, 1)) MB | $($files.Count) 个 | $($files[0].FullName) |}}if ($dupCount -eq 0) {$reportLines | — | 未发现大于 1MB 的重复文件 | — |}$reportLines $reportLines **可释放空间估算**: $([math]::Round($totalDuplicateSize/1MB, 1)) MB$reportLines $reportLines ---$reportLines # ════════════════════════════════════════# 2. 系统临时文件# ════════════════════════════════════════$reportLines ## 2. 系统临时文件$reportLines $tempPaths ($env:TEMP,$env:WINDIR\Temp,$env:USERPROFILE\AppData\Local\Temp)$totalTempSize 0foreach ($tmp in $tempPaths) {if (-not (Test-Path $tmp)) { continue }$size (Get-ChildItem -Path $tmp -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum$sizeMB [math]::Round(($size / 1MB), 1)$totalTempSize $size$reportLines - **$tmp**: $sizeMB MB}$reportLines $reportLines **临时文件总计**: $([math]::Round($totalTempSize/1MB, 1)) MB$reportLines $reportLines ---$reportLines # ════════════════════════════════════════# 3. 回收站状态# ════════════════════════════════════════$reportLines ## 3. 回收站$reportLines $shell New-Object -ComObject Shell.Application$recycle $shell.NameSpace(0xa)$recycleItems $recycle.Items()$reportLines - **回收站中文件数**: $($recycleItems.Count)$reportLines - 清空回收站命令: Clear-RecycleBin -Force$reportLines $reportLines ---$reportLines # ════════════════════════════════════════# 4. 大文件排行 (Top 20)# ════════════════════════════════════════$reportLines ## 4. 大文件排行 (Top 20)$reportLines $reportLines | 大小 | 路径 |$reportLines |------|------|$largeFiles ()foreach ($path in $ScanPaths) {if (-not (Test-Path $path)) { continue }$largeFiles Get-ChildItem -Path $path -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.Length -gt 100MB }}$largeFiles $largeFiles | Sort-Object -Property Length -Descending | Select-Object -First 20if ($largeFiles.Count -eq 0) {$reportLines | — | 未发现超过 100MB 的大文件 |} else {foreach ($f in $largeFiles) {$reportLines | $([math]::Round($f.Length/1MB, 1)) MB | $($f.FullName) |}}$reportLines $reportLines ---$reportLines # ════════════════════════════════════════# 5. 常见可清理目录# ════════════════════════════════════════$reportLines ## 5. 常见可清理目录$reportLines $reportLines 以下目录通常可安全清理$reportLines $cleanupTargets ({Path $env:LOCALAPPDATA\Microsoft\Windows\INetCache; Desc Internet Explorer / Edge 缓存},{Path $env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache; Desc Edge 浏览器缓存},{Path $env:USERPROFILE\.npm\_cacache; Desc npm 缓存},{Path $env:USERPROFILE\.cache; Desc 通用缓存 (WSL / 各种工具)},{Path $env:WINDIR\SoftwareDistribution\Download; Desc Windows Update 下载缓存 (清除后下次更新需重新下载)})foreach ($t in $cleanupTargets) {$exists Test-Path $t.Pathif ($exists) {$size (Get-ChildItem -Path $t.Path -Recurse -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum$sizeMB [math]::Round(($size / 1MB), 1)if ($sizeMB -gt 0) {$reportLines - [$($t.Desc)]($($t.Path)) — **$sizeMB MB**} else {$reportLines - [$($t.Desc)]($($t.Path)) — *空*}} else {$reportLines - [$($t.Desc)]($($t.Path)) — *目录不存在*}}$reportLines $reportLines ---$reportLines # ════════════════════════════════════════# 6. 清理建议# ════════════════════════════════════════$reportLines ## 6. 清理建议$reportLines $reportLines ### 安全清理一键执行$reportLines $reportLines powershell$reportLines # 清空当前用户临时文件夹$reportLines Remove-Item $env:TEMP\* -Recurse -Force -ErrorAction SilentlyContinue$reportLines $reportLines # 清空系统临时文件夹 (需管理员)$reportLines # Remove-Item $env:WINDIR\Temp\* -Recurse -Force -ErrorAction SilentlyContinue$reportLines $reportLines # 清空回收站$reportLines Clear-RecycleBin -Force$reportLines $reportLines $reportLines ### Windows 自带磁盘清理$reportLines $reportLines 1. 按 Win R → 输入 cleanmgr → 回车$reportLines 2. 选择要清理的驱动器$reportLines 3. 点击「清理系统文件」可获取更多选项$reportLines $reportLines ### 推荐工具$reportLines $reportLines - **WizTree** — 可视化磁盘空间分析比 TreeSize 快数倍$reportLines - **CCleaner** (注意安装时取消捆绑) — 注册表/临时文件清理$reportLines - **dupeGuru** / **czkawka** — 开源重复文件查找$reportLines $reportLines ---$reportLines # ════════════════════════════════════════# 写入报告# ════════════════════════════════════════$reportContent $reportLines -join n$reportContent | Out-File -FilePath $ReportPath -Encoding UTF8Write-Host -ForegroundColor CyanWrite-Host 扫描完成 -ForegroundColor GreenWrite-Host 报告已保存至: $ReportPath -ForegroundColor GreenWrite-Host -ForegroundColor CyanWrite-Host Write-Host 扫描摘要: -ForegroundColor YellowWrite-Host - 重复文件组: $dupCount (可释放约 $([math]::Round($totalDuplicateSize/1MB, 1)) MB)Write-Host - 临时文件: $([math]::Round($totalTempSize/1MB, 1)) MBWrite-Host - 回收站项目: $($recycleItems.Count)# ════════════════════════════════════════# 可选清理动作# ════════════════════════════════════════if (-not $DryRun) {Write-Host Write-Host 正在执行清理... -ForegroundColor Redif ($CleanTemp) {Write-Host - 清理临时文件... -NoNewlineRemove-Item $env:TEMP\* -Recurse -Force -ErrorAction SilentlyContinueWrite-Host 完成 -ForegroundColor Green}if ($CleanRecycleBin) {Write-Host - 清空回收站... -NoNewlineClear-RecycleBin -Force -ErrorAction SilentlyContinueWrite-Host 完成 -ForegroundColor Green}Write-Host 清理完成 -ForegroundColor Green}