Windows C 盘深度清理实战:PowerShell 分析脚本 + 踩坑记录 + 清理命令汇总

C 盘 117.8GB,剩余 1.04GB,系统频繁提示空间不足。本文记录一次完整的 C 盘分析与清理过程,包括分析脚本、实际遇到的报错和解决方法、分类清理命令,所有命令均可直接复制执行。

1. 空间分析脚本

清理前先分析。写了一个 PowerShell 脚本,输出 C 盘基本信息、根目录文件夹排行、Windows 目录详情、用户目录详情、AppData\Local 详情和大文件列表。

第一版脚本用 Get-ChildItem -Recurse 直接管道给 Measure-Object,遇到空文件夹时报错"在任何对象的输入中都找不到属性 Length"。原因是空文件夹的递归结果里没有文件对象,Measure-Object 找不到 Length 属性。修复方法是加-File 参数过滤,并封装成函数处理 null 值。

function Get-FolderSize($path) {
    $sum = (Get-ChildItem $path -Recurse -Force -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
    if ($null -eq $sum) { $sum = 0 }
    return [math]::Round($sum/1GB,2)
}
Write-Host "===== C Drive Analysis =====" -ForegroundColor Cyan
$drive = Get-PSDrive C
Write-Host "[1] Total: $([math]::Round(($drive.Used+$drive.Free)/1GB,2))GB, Used: $([math]::Round($drive.Used/1GB,2))GB, Free: $([math]::Round($drive.Free/1GB,2))GB"
Write-Host "`n[2] Top folders under C:\" -ForegroundColor Yellow
Get-ChildItem C:\ -Directory -Force -ErrorAction SilentlyContinue | ForEach-Object {
    [PSCustomObject]@{ Folder=$_.Name; SizeGB=(Get-FolderSize $_.FullName) }
} | Sort-Object SizeGB -Descending | Select-Object -First 15 | Format-Table -AutoSize
Write-Host "`n[3] Windows folder breakdown" -ForegroundColor Yellow
Get-ChildItem C:\Windows -Directory -Force -ErrorAction SilentlyContinue | ForEach-Object {
    [PSCustomObject]@{ Folder=$_.Name; SizeGB=(Get-FolderSize $_.FullName) }
} | Sort-Object SizeGB -Descending | Select-Object -First 10 | Format-Table -AutoSize
Write-Host "`n[4] User profile folders" -ForegroundColor Yellow
Get-ChildItem $env:USERPROFILE -Directory -Force -ErrorAction SilentlyContinue | ForEach-Object {
    [PSCustomObject]@{ Folder=$_.Name; SizeGB=(Get-FolderSize $_.FullName) }
} | Sort-Object SizeGB -Descending | Select-Object -First 10 | Format-Table -AutoSize
Write-Host "`n[5] AppData\Local folders" -ForegroundColor Yellow
Get-ChildItem "$env:LOCALAPPDATA" -Directory -Force -ErrorAction SilentlyContinue | ForEach-Object {
    [PSCustomObject]@{ Folder=$_.Name; SizeGB=(Get-FolderSize $_.FullName) }
} | Sort-Object SizeGB -Descending | Select-Object -First 15 | Format-Table -AutoSize
Write-Host "`n[6] Top 20 large files (>100MB)" -ForegroundColor Yellow
Get-ChildItem C:\ -Recurse -Force -File -ErrorAction SilentlyContinue | Where-Object { $_.Length -gt 100MB } | Sort-Object Length -Descending | Select-Object -First 20 | ForEach-Object {
    [PSCustomObject]@{ SizeGB=[math]::Round($_.Length/1GB,2); Path=$_.FullName }
} | Format-Table -AutoSize
Write-Host "`n===== Done =====" -ForegroundColor Green

注意:必须以管理员身份运行,否则 C:\Windows 下部分目录无法访问。脚本只读不写。

2. 分析结果

目录 大小 说明
Users 53.94GB AppData 占 41.9GB,为最大占用源
Windows 39.69GB System32 12.37GB、Installer 11.42GB、WinSxS 8.29GB
Program Files 25.73GB 64 位程序
Program Files (x86) 14.19GB 32 位程序,含多个 Edge 版本
ProgramData 8.84GB 含 MySQL 数据 5.7、Windows 搜索索引等

AppData\Local 主要占用:JetBrains 3.48GB、Kingsoft(WPS) 2.74GB、Programs 2.59GB、Microsoft 2.4GB、npm-cache 1.48GB、微信开发者工具 1.26GB、Doubao 1.25GB。

大文件中发现一个 2.72GB 的系统崩溃转储:C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp\4909119c-*.dmp

3. 踩坑记录

​坑 1:分析脚本报错。​Get-ChildItem -Recurse 管道给 Measure-Object -Property Length 时,空文件夹导致没有文件对象输入,Measure-Object 报错。解决:加-File 参数,封装函数处理$sum 为$null 的情况。

​坑 2:删除文件夹提示文件被占用。​删除.qoder-cn 时,atlas.db、atlas.db-shm、atlas.db-wal 三个文件被 Qoder 后台进程锁定。解决:先 taskkill 杀掉 qoder.exe 等进程,等待 2 秒后再删。

​坑 3:ServiceProfiles 路径不存在。​直接 Remove-Item "C:\Windows\ServiceProfiles\LocalService\AppData\Local\Temp\*.dmp"报路径不存在。原因:非管理员权限无法访问该目录,且 Temp 目录可能已被系统清理。解决:用管理员 PowerShell,改用 Get-ChildItem 递归搜索*.dmp 再删除。

​坑 4:卸载程序报错找不到文件。​Razer Cortex、Razer Synapse、微信的卸载条目指向的 exe 文件已不存在(之前可能直接删了文件夹),控制面板卸载时报"Windows 找不到文件"。解决:检测坏条目后删除对应注册表项。

4. 清理命令汇总

开发工具缓存:

# AI编程助手缓存(先关闭相关程序和VS Code)
taskkill /F /IM qoder.exe 2>$null
taskkill /F /IM lingma.exe 2>$null
taskkill /F /IM trae.exe 2>$null
Start-Sleep 2
Remove-Item -Recurse -Force "$env:USERPROFILE\.qoder-cn","$env:USERPROFILE\.qoder","$env:USERPROFILE\.lingma","$env:USERPROFILE\.trae","$env:USERPROFILE\.trae-cn","$env:USERPROFILE\.codex","$env:USERPROFILE\.cache" -ErrorAction SilentlyContinue
# npm缓存清理+迁移D盘
npm cache clean --force
npm config set cache "D:\npm-cache" --global
# pip/poetry缓存(如有)
Remove-Item -Recurse -Force "$env:LOCALAPPDATA\pypoetry\Cache\*" -ErrorAction SilentlyContinue

系统临时文件与转储:

Remove-Item "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
Remove-Item "C:\Windows\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
Get-ChildItem "C:\Windows\ServiceProfiles" -Recurse -Force -Filter *.dmp -ErrorAction SilentlyContinue | Remove-Item -Force -ErrorAction SilentlyContinue
Remove-Item "C:\Windows\Prefetch\*" -Force -ErrorAction SilentlyContinue
Clear-RecycleBin -Force -ErrorAction SilentlyContinue

Windows 更新缓存:

Stop-Service wuauserv -Force
Remove-Item "C:\Windows\SoftwareDistribution\Download\*" -Recurse -Force -ErrorAction SilentlyContinue
Start-Service wuauserv

DISM 组件存储清理:

# 基础清理(推荐)
Dism /Online /Cleanup-Image /StartComponentCleanup
# 深度清理(/ResetBase,清理后无法卸载已安装更新,谨慎)
# Dism /Online /Cleanup-Image /StartComponentCleanup /ResetBase

5. 注册表残留清理

检测卸载程序已丢失的坏条目,确认后删除:

# 检测
Get-ItemProperty "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*","HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*" -ErrorAction SilentlyContinue | Where-Object { $_.DisplayName -and $_.UninstallString } | ForEach-Object {
    $exePath = $null
    if ($_.UninstallString -match '"([^"]+\.exe)"') { $exePath = $Matches[1] }
    elseif ($_.UninstallString -match '([A-Za-z]:\\[^ ]+\.exe)') { $exePath = $Matches[1] }
    if ($exePath -and !(Test-Path $exePath)) {
        Write-Host "BROKEN: $($_.DisplayName)"
        Write-Host "  RegPath: $($_.PSPath)"
    }
}
# 删除示例(替换为实际检测到的路径)
# Remove-Item "HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Razer Cortex_is1" -Recurse -Force

6. 注意事项

  • C:\Windows\Installer 不可直接删除,需用 PatchCleaner 识别冗余项
  • hiberfil.sys 可通过 powercfg /h off 关闭(会失去快速启动)
  • pagefile.sys 不建议删除,可调大小或迁移
  • Outlook OST 删除后自动重建,但需重新同步邮件
  • JetBrains 缓存删除后首次打开 IDE 会重建索引,耗时较长
  • 软件迁移到 D 盘需先卸载再重装,不可直接剪切文件夹

7. 结果

$d = Get-PSDrive C
Write-Host "Free: $([math]::Round($d.Free/1GB,2))GB / Total: $([math]::Round(($d.Used+$d.Free)/1GB,2))GB"

清理前 1.04GB,清理后 13.88GB,释放约 12.8GB。进一步卸载大型软件和清理 Installer 可释放 20GB+。

posted @ 2026-08-28 11:37  kaizi1992  阅读(7)  评论(0)    收藏  举报