C盘深度清理:从手动操作到脚本自动化
文章目录
由
Deepseek提供支持
广告
cleanmgr.exe是微软Windows系统内置的磁盘清理工具,用于通过删除临时文件、回收站内容和系统更新文件等冗余数据释放磁盘空间,支持Windows 7 SP1至Windows 11及对应服务器版本系统
1.参数
| 参数 | 描述 |
|---|---|
/d <driveletter> | 指定要清理磁盘的驱动器。注意:/d 选项不与 /sagerun:n一起使用。 |
| /sageset:n | 显示“磁盘清理设置”对话框,并创建用于存储所选设置的注册表项。 n 值存储在注册表中,可用于指定要运行磁盘清理的任务。 n 值可以是 0 到 9999 的任何整数值。 有关详细信息,请参阅注册表项信息。 |
| /sagerun:n | 如果使用 /sageset 选项,则运行分配给 n 值的指定任务。 将枚举计算机上的所有驱动器,并且所选配置文件针对每个驱动器运行。 |
| /tuneup:n | 为相同的 .n |
| /lowdisk | 使用默认设置运行。 |
| /verylowdisk | 使用默认设置运行而不提示用户。 |
| /autoclean | 自动删除升级 Windows 后留下的文件。 |
| /? | 在命令提示符下显示帮助。 |
2.例子
若要运行“磁盘清理”应用程序,以便可以使用其对话框指定以后使用的选项,将设置存储到集合 1,请键入以下内容
cleanmgr /sageset:1
若要运行磁盘清理并包含使用 cleanmgr /sageset:1 命令指定的选项
cleanmgr /sagerun:1
若要同时运行 cleanmgr /sageset:1 和 cleanmgr /sagerun:1
cleanmgr /tuneup:1
3.递归删除
Get-ChildItem "C:\Windows\SoftwareDistribution\Download" -Recurse |
Where-Object { $_.LastWriteTime -lt [DateTime]::Now.AddDays(-30) } | # 仅删除30天前的文件
Remove-Item -Force -ErrorAction SilentlyContinue # 忽略不存在的文件错误
4.Powershell脚本删除
# 清理Windows更新残留及系统文件(无需注册表操作)
# 请以管理员身份运行此脚本
Write-Host "开始清理Windows更新残留及系统文件..." -ForegroundColor Cyan
# 使用DISM工具清理系统更新文件
Write-Host "`n使用DISM工具清理系统更新文件..." -ForegroundColor Yellow
try {
# 清理WinSxS文件夹中的更新残留
DISM /Online /Cleanup-Image /StartComponentCleanup /ResetBase
Write-Host "DISM清理完成" -ForegroundColor Green
}
catch {
Write-Host "DISM清理时出错: $_" -ForegroundColor Red
}
# 清理Windows Update缓存(已安装的更新安装包)
Write-Host "`n清理Windows Update下载缓存..." -ForegroundColor Yellow
$updateCachePath = "C:\Windows\SoftwareDistribution\Download"
if (Test-Path $updateCachePath) {
Remove-Item -Path $updateCachePath\* -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Windows Update缓存清理完成" -ForegroundColor Green
}
else {
Write-Host "未找到Windows Update缓存目录" -ForegroundColor Yellow
}
# 清理Windows升级遗留文件(Win10升级Win11后产生)
Write-Host "`n清理Windows 10升级到Windows 11的遗留文件..." -ForegroundColor Yellow
# Windows.old文件夹(包含旧系统文件)
if (Test-Path "C:\Windows.old") {
# 使用系统命令清理Windows.old
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/VERYLOWDISK" -Wait -NoNewWindow
Write-Host "已清理Windows.old文件夹(旧系统文件)" -ForegroundColor Green
}
else {
Write-Host "未找到Windows.old文件夹" -ForegroundColor Yellow
}
# 清理升级过程中产生的临时文件
$upgradeTempPaths = @(
"C:\$Windows.~BT", # 升级临时文件
"C:\$Windows.~WS", # 升级源文件缓存
"C:\Recovery\WindowsRE", # 旧系统恢复环境
"C:\ProgramData\Microsoft\Windows\Previous Versions"
)
foreach ($path in $upgradeTempPaths) {
if (Test-Path $path) {
Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "已清理升级残留文件: $path" -ForegroundColor Green
}
else {
Write-Host "未找到升级残留文件: $path" -ForegroundColor Yellow
}
}
# 运行磁盘清理工具清理系统文件(通过命令行参数直接触发)
Write-Host "`n运行磁盘清理工具清理系统文件..." -ForegroundColor Yellow
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait -NoNewWindow
Write-Host "`n所有清理操作已完成!" -ForegroundColor Green
Write-Host "建议重启计算机以释放所有被占用的空间。" -ForegroundColor Cyan
温馨提示 : 非特殊注明,否则均为©李联华的博客网原创文章,本站文章未经授权禁止任何形式转载;来自:俄亥俄州·哥伦布 ,欢迎您的访问!
文章链接:https://www.lilianhua.com/c-disk-deep-cleaning-from-manual-operation-to-script.html
文章链接:https://www.lilianhua.com/c-disk-deep-cleaning-from-manual-operation-to-script.html
English (US)
Español (ES)
Português (PT)
Français (CA)
Español (MX)
Español (VE)
Español (CO)
Español (AR)
Português (BR)
Quechua (PE)
Guaraní (PY)
简体中文 (ZH)
繁體中文 (HK)
日本語 (JP)
한국어 (KR)
हिन्दी (HI)
Pilipino (PH)
ไทย (TH)
Tiếng Việt (VN)
Bahasa Melayu (MY)
Bahasa Indonesia (ID)
বাংলা (BD)
اردو (PK)
සිංහල (LK)
ភាសាខ្មែរ (KH)
English (UK)
Français (FR)
Deutsch (DE)
Italiano (IT)
Русский (RU)
Nederlands (NL)
Türkçe (TR)
Polski (PL)
Svenska (SE)
Norsk (NO)
Dansk (DK)
Suomi (FI)
Ελληνικά (GR)
Čeština (CZ)
Magyar (HU)
Română (RO)
Български (BG)
Српски (RS)
Українська (UA)

