硬盘空间管理助手
扫描 Windows 系统中的各类缓存和垃圾文件,列出占用空间并按你的选择进行清理。
使用方式
直接说"清理磁盘"、"磁盘空间不足"或"扫描垃圾文件"即可触发。你随时可以指定只清理某几类(比如"只清理浏览器缓存")或预览后再决定。
需要管理员权限才能彻底清理所有项目。如果当前终端没有管理员权限,会提示你哪些项目需要提权后运行。
扫描流程
执行以下函数进行扫描,计算每个类目的大小并汇总输出表格。
扫描函数
function Get-DiskScanReport {
$items = @()
# 辅助函数:计算目录大小
function Get-DirSize {
param([string]$Path)
if (Test-Path $Path) {
try {
$size = (Get-ChildItem -Path $Path -Recurse -Force -ErrorAction Stop |
Measure-Object -Property Length -Sum -ErrorAction Stop).Sum
if ($size -gt 0) { return [math]::Round($size / 1MB, 2) } else { return 0 }
} catch {
return -1 # 权限不足
}
}
return $null # 路径不存在
}
# 定义扫描项:标签、路径、风险等级
$scanTargets = @(
@{ Label = "用户临时文件"; Path = "$env:TEMP"; Risk = "安全" }
@{ Label = "系统临时文件"; Path = "C:\Windows\Temp"; Risk = "安全" }
@{ Label = "Internet 缓存"; Path = "$env:LOCALAPPDATA\Microsoft\Windows\INetCache"; Risk = "安全" }
@{ Label = "Chrome 缓存"; Path = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache"; Risk = "安全" }
@{ Label = "Edge 缓存"; Path = "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache"; Risk = "安全" }
@{ Label = "Firefox 缓存"; Path = "$env:APPDATA\Mozilla\Firefox\Profiles\*\cache2"; Risk = "安全" }
@{ Label = "npm 缓存"; Path = "$env:APPDATA\npm-cache"; Risk = "安全" }
@{ Label = "pip 缓存"; Path = "$env:LOCALAPPDATA\pip\cache"; Risk = "安全" }
@{ Label = "回收站"; Path = "C:\`$Recycle.Bin"; Risk = "谨慎" }
@{ Label = "崩溃转储"; Path = "$env:LOCALAPPDATA\CrashDumps"; Risk = "安全" }
@{ Label = "Windows 预取文件"; Path = "C:\Windows\Prefetch"; Risk = "安全" }
@{ Label = "Windows 更新缓存"; Path = "C:\Windows\SoftwareDistribution\Download"; Risk = "谨慎" }
@{ Label = "错误报告"; Path = "$env:LOCALAPPDATA\Microsoft\Windows\WER"; Risk = "安全" }
@{ Label = "VS Code 缓存"; Path = "$env:APPDATA\Code\Cache"; Risk = "安全" }
@{ Label = "VS Code 缓存数据"; Path = "$env:APPDATA\Code\CachedData"; Risk = "安全" }
@{ Label = "VS Code 扩展缓存"; Path = "$env:APPDATA\Code\CachedExtensions"; Risk = "安全" }
@{ Label = "缩略图缓存"; Path = "$env:LOCALAPPDATA\Microsoft\Windows\Explorer"; Risk = "安全" }
@{ Label = "Docker 缓存"; Path = "$env:APPDATA\Docker"; Risk = "安全" }
@{ Label = "NuGet 缓存"; Path = "$env:USERPROFILE\.nuget\packages"; Risk = "谨慎" }
@{ Label = "Steam 着色器缓存"; Path = "$env:USERPROFILE\AppData\Local\Steam\htmlcache"; Risk = "安全" }
)
$results = @()
foreach ($target in $scanTargets) {
$size = Get-DirSize -Path $target.Path
if ($size -eq $null) {
# 路径不存在,跳过
continue
}
$displaySize = if ($size -eq -1) { "权限不足" } else { "$size MB" }
Write-Progress -Activity "正在扫描" -Status $target.Label -PercentComplete (($results.Count / $scanTargets.Count) * 100)
$results += [PSCustomObject]@{
类目 = $target.Label
大小 = if ($size -eq -1) { -1 } else { $size }
显示大小 = $displaySize
风险 = $target.Risk
路径 = $target.Path
}
}
# 按大小降序排列(可计算的)
$sorted = $results | Where-Object { $_.大小 -ge 0 } | Sort-Object 大小 -Descending
$noAccess = $results | Where-Object { $_.大小 -eq -1 }
# 输出汇总表格
Write-Host "`n========== 磁盘空间扫描报告 ==========" -ForegroundColor Cyan
if ($sorted.Count -gt 0) {
$sorted | Format-Table 类目, @{N="大小(MB)";E={"{0:N2}" -f $_.大小}}, 风险 -AutoSize
}
if ($noAccess.Count -gt 0) {
Write-Host "`n以下项目因权限不足无法扫描(请尝试以管理员身份运行):" -ForegroundColor Yellow
$noAccess | ForEach-Object { Write-Host " - $($_.类目)" }
}
$totalSize = ($sorted | Measure-Object 大小 -Sum).Sum
Write-Host "`n总计可释放空间:$([math]::Round($totalSize, 2)) MB" -ForegroundColor Green
Write-Host "======================================`n" -ForegroundColor Cyan
return $results
}
执行 Get-DiskScanReport 进行扫描并展示结果。
清理流程
步骤 1:显示扫描结果
调用 Get-DiskScanReport 展示所有可清理项目及其大小、风险等级。
风险等级说明: | 等级 | 说明 | |------|------| | 安全 | 删除后不影响系统正常运行,可放心清理 | | 谨慎 | 清理后可能影响某些功能(如更新缓存需重新下载),建议确认后再操作 |
步骤 2:用户选择
询问用户想清理哪些项目:
- 全部清理 - 清理所有"安全"项目,"谨慎"项目逐个再次确认
- 按编号选择 - 列出编号让用户输入(如
1,3,5-7) - 仅清理风险项 - 只清理"安全"类项目
- 取消 - 不做任何操作
步骤 3:执行清理
function Invoke-DiskCleanup {
param([array]$SelectedItems)
$totalFreed = 0
$errors = @()
$skipped = @()
foreach ($item in $SelectedItems) {
if (-not (Test-Path $item.路径)) {
Write-Host "[跳过] $($item.类目) — 路径不存在" -ForegroundColor DarkGray
continue
}
Write-Host "[清理] $($item.类目)..." -NoNewline
try {
$before = (Get-ChildItem $item.路径 -Recurse -Force -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum -ErrorAction SilentlyContinue).Sum
if (-not $before) { $before = 0 }
if ($item.类目 -eq "回收站") {
Clear-RecycleBin -Force -ErrorAction Stop
} else {
Remove-Item "$($item.路径)\*" -Recurse -Force -ErrorAction Stop
}
Write-Host " 完成" -ForegroundColor Green
$freed = [math]::Round($before / 1MB, 2)
$totalFreed += $freed
Write-Host " 释放空间: $freed MB" -ForegroundColor DarkGreen
} catch {
if ($_ -match "AccessDenied|UnauthorizedAccess|PermissionDenied") {
Write-Host " 权限不足,跳过" -ForegroundColor Yellow
$skipped += $item.类目
} elseif ($_ -match "DirectoryNotFound|PathNotFound") {
Write-Host " 路径不存在,跳过" -ForegroundColor DarkGray
} else {
Write-Host " 失败" -ForegroundColor Red
Write-Host " 原因: $($_.Exception.Message)" -ForegroundColor DarkRed
$errors += @{ 类目 = $item.类目; 原因 = $_.Exception.Message }
}
}
}
Write-Host "`n========== 清理报告 ==========" -ForegroundColor Cyan
Write-Host "成功释放: $totalFreed MB" -ForegroundColor Green
if ($skipped.Count -gt 0) {
Write-Host "因权限跳过: $($skipped -join ', ')" -ForegroundColor Yellow
Write-Host "提示:以管理员身份运行可清理上述项目" -ForegroundColor Yellow
}
if ($errors.Count -gt 0) {
Write-Host "清理失败:" -ForegroundColor Red
foreach ($e in $errors) {
Write-Host " - $($e.类目): $($e.原因)" -ForegroundColor DarkRed
}
}
Write-Host "=============================`n" -ForegroundColor Cyan
}
调用方式:
Invoke-DiskCleanup -SelectedItems $selectedItems
执行逻辑
- 对每个被选项目,先计算清理前大小
- 执行删除(回收站用
Clear-RecycleBin,其余用Remove-Item) - 计算释放空间,累加到总计
- 遇到错误时区分权限不足、路径不存在、其他错误,分别给出明确提示
- 最终输出清理报告,告知释放总量、跳过项目和失败原因
使用示例
用户: "清理下磁盘" Claude 执行: 调用 Get-DiskScanReport 扫描 输出:
========== 磁盘空间扫描报告 ==========
类目 大小(MB) 风险
---- -------- ---
Windows 更新缓存 1245.30 谨慎
用户临时文件 342.15 安全
Chrome 缓存 128.40 安全
npm 缓存 89.20 安全
回收站 45.00 谨慎
...
总计可释放空间:1890.05 MB
======================================
以下项目因权限不足无法扫描(请尝试以管理员身份运行):
- 系统临时文件
Claude 提示: "共发现约 1.89 GB 可清理空间。要清理哪些项目?(全部/按编号选择/仅清理安全项/取消)"
安全注意事项
- 只清理明确标记为缓存/临时/日志的目录,不动系统关键文件
- 每次删除前先确认路径存在
- 权限不足的项目跳过并提示,不强制删除
- Windows 更新缓存清理后,下次更新需重新下载,需告知用户
- 被进程占用的文件会自动跳过,不影响其他文件清理
- 回收站清理不可撤销,删除前二次确认
- 建议以管理员身份运行以获得最彻底的清理效果
常见问题 (FAQ)
| 问题 | 回答 | |------|------| | 这个工具在 Mac/Linux 上能用吗? | 不能,仅支持 Windows 系统 | | 清理前需要备份吗? | 系统缓存和临时文件无需备份,"谨慎"类目建议确认后再清理 | | 为什么有些项目显示"权限不足"? | 某些系统目录需要管理员权限才能访问,可以右键以管理员身份运行终端 | | 清理后系统变慢了? | 通常不会。清理后首次运行某些程序可能稍慢(需要重新生成缓存),之后恢复正常 | | 浏览器缓存清理后有什么影响? | 需要重新登录已登录的网站,首次加载页面稍慢 | | 如何查看每个项目具体包含哪些文件? | 当前只显示总大小,如果需要可以展开具体路径 | | 清理到一半能中断吗? | 可以,直接关闭终端或按 Ctrl+C,已清理的部分不会回滚 |
输出格式
| 前缀 | 用途 | 示例 |
|------|------|------|
| [扫描] | 正在扫描的类目 | [扫描] 正在扫描 Chrome 缓存... |
| [发现] | 扫描结果 | [发现] npm 缓存: 89.20 MB |
| [清理] | 正在执行清理 | [清理] 用户临时文件... |
| [完成] | 清理完成及释放空间 | [完成] 释放 342.15 MB |
| [跳过] | 路径不存在或权限不足 | [跳过] 系统临时文件 — 权限不足 |
| [警告] | 需要注意的事项 | [警告] 清理更新缓存后需重新下载 |
| [错误] | 清理失败及原因 | [错误] xxx: 文件被占用 |
Scan to join WeChat group