Windows 11系统清理脚本

一、脚本内容

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
<#
.SYNOPSIS
Windows 11 系统垃圾文件一键清理脚本(最终版)
.DESCRIPTION
清理用户级Temp、系统级Temp、C:\TEMP、更新缓存、日志、预读取文件等,自动跳过占用文件
.NOTES
运行要求:管理员权限,Windows 11 任意版本
#>

# ========== 核心:强制设置编码为 UTF-8,解决中文乱码 ==========
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

# ========== 检查管理员权限 ==========
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Host "`n⚠️  请以【管理员身份】运行此脚本!" -ForegroundColor Red
    Write-Host "提示:右键 PowerShell → 以管理员身份运行" -ForegroundColor Yellow
    Read-Host -Prompt "按任意键退出"
    exit 1
}

# ========== 清理流程 ==========
Write-Host "`n========================================" -ForegroundColor Cyan
Write-Host "      Windows 11 系统垃圾一键清理(最终版)" -ForegroundColor Cyan
Write-Host "========================================`n" -ForegroundColor Cyan

# 1. 清理用户级 Temp 文件夹
Write-Host "🔧 正在清理【用户级临时文件】(%temp%)..." -ForegroundColor Green
try {
    Remove-Item -Path "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "✅ 用户级 Temp 清理完成`n" -ForegroundColor Green
}
catch {
    Write-Host "⚠️ 用户级 Temp 部分文件被占用,已跳过`n" -ForegroundColor Yellow
}

# 2. 清理系统级 Temp 文件夹(C:\Windows\Temp)
Write-Host "🔧 正在清理【系统级临时文件】(C:\Windows\Temp)..." -ForegroundColor Green
try {
    Remove-Item -Path "$env:WINDIR\Temp\*" -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "✅ 系统级 Temp 清理完成`n" -ForegroundColor Green
}
catch {
    Write-Host "⚠️ 系统级 Temp 部分文件被占用,已跳过`n" -ForegroundColor Yellow
}

# 3. 清理系统兜底 Temp 文件夹(C:\TEMP)
Write-Host "🔧 正在清理【系统兜底临时文件】(C:\TEMP)..." -ForegroundColor Green
try {
    if (Test-Path "C:\TEMP") {
        Remove-Item -Path "C:\TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue
    }
    Write-Host "✅ C:\TEMP 清理完成`n" -ForegroundColor Green
}
catch {
    Write-Host "⚠️ C:\TEMP 部分文件被占用,已跳过`n" -ForegroundColor Yellow
}

# 4. 清理 Windows 更新缓存
Write-Host "🔧 正在清理【Windows 更新缓存】(SoftwareDistribution\Download)..." -ForegroundColor Green
try {
    # 停止更新服务(避免文件占用)
    Stop-Service -Name wuauserv -Force -ErrorAction SilentlyContinue
    Remove-Item -Path "$env:WINDIR\SoftwareDistribution\Download\*" -Recurse -Force -ErrorAction SilentlyContinue
    # 重启更新服务
    Start-Service -Name wuauserv -ErrorAction SilentlyContinue
    Write-Host "✅ 更新缓存清理完成`n" -ForegroundColor Green
}
catch {
    Write-Host "⚠️ 更新缓存部分文件被占用,已跳过`n" -ForegroundColor Yellow
}

# 5. 清理预读取文件
Write-Host "🔧 正在清理【预读取文件】(C:\Windows\Prefetch)..." -ForegroundColor Green
try {
    Remove-Item -Path "$env:WINDIR\Prefetch\*" -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "✅ 预读取文件清理完成`n" -ForegroundColor Green
}
catch {
    Write-Host "⚠️ 预读取文件部分被占用,已跳过`n" -ForegroundColor Yellow
}

# 6. 清理系统日志文件
Write-Host "🔧 正在清理【系统日志文件】(C:\Windows\Logs)..." -ForegroundColor Green
try {
    Remove-Item -Path "$env:WINDIR\Logs\*" -Recurse -Force -ErrorAction SilentlyContinue
    Write-Host "✅ 系统日志清理完成`n" -ForegroundColor Green
}
catch {
    Write-Host "⚠️ 系统日志部分文件被占用,已跳过`n" -ForegroundColor Yellow
}

# ========== 清理完成 ==========
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "🎉 所有垃圾文件清理完成!" -ForegroundColor Green
Write-Host "💡 说明:部分文件因被系统/程序占用未删除,重启后可再次运行" -ForegroundColor Yellow
Write-Host "========================================`n" -ForegroundColor Cyan

Read-Host -Prompt "按任意键退出脚本"

二、使用步骤

步骤 1:创建脚本文件

  1. 新建文本文档,将上面的脚本完整复制粘贴进去。
  2. 点击「文件 → 另存为」:
    • 文件名:CleanWin11Junk.ps1(后缀必须是 .ps1,不是 .txt
    • 保存类型:「所有文件」
    • 编码:选择「UTF-8 with BOM」(关键!解决乱码核心)
    • 保存位置:桌面(方便查找)

步骤 2:运行脚本(管理员权限)

  1. Win+X,选择「Windows PowerShell (管理员)」(或「终端 (管理员)」)。
  2. 切换到脚本保存路径(如桌面):
    1
    
    cd Desktop
  3. 执行脚本(若提示执行策略限制,先运行下方命令):
    1
    2
    3
    4
    
    # 临时允许运行脚本(仅本次会话有效,安全无风险)
    Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
    # 运行清理脚本
    .\CleanWin11Junk.ps1

三、注意事项

  1. 安全保障:脚本仅删除临时文件/缓存/日志,不涉及系统关键文件,可放心运行。
  2. 文件占用:清理时提示“跳过占用文件”是正常现象,重启电脑后再次运行即可清理。
  3. 执行策略:若提示“无法加载脚本”,是 Windows 默认执行策略限制,步骤 2 中的 Set-ExecutionPolicy 命令已临时解除,无需修改系统全局策略。
原文链接: https://www.17you.com/tool/windows11%E7%B3%BB%E7%BB%9F%E5%9E%83%E5%9C%BE%E4%B8%80%E9%94%AE%E6%B8%85%E7%90%86%E8%84%9A%E6%9C%AC/ 已复制!
脚本编程和自动化工具

寻找技术支持帮助和技术合伙人一起搞事。

请点击联系我


相关内容