前言
每次清理C盘都很麻烦,为了不用下载软件就能更方便的清理,所以写了清理脚本。
第一步:新建Clear.ps1文件
新建记事本,将下面的代码粘贴进去另存为Clear.ps1
,编码选择ANSI
然后保存。
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
# 记录清理前的空间
function Get-DirectorySize {
param (
[string]$path
)
if (Test-Path $path) {
return (Get-ChildItem $path -Recurse -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum).Sum
}
return 0
}
$totalFreedSpace = 0
# 清理临时文件
$TempPath = "$env:Temp\*"
$initialTempSize = Get-DirectorySize "$env:Temp"
Remove-Item $TempPath -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialTempSize
Write-Host "临时文件已清理。"
# 清空回收站
Clear-RecycleBin -Force -ErrorAction SilentlyContinue
Write-Host "回收站已清空。"
# 删除 Windows 更新临时文件
$WindowsUpdateTemp = "C:\Windows\SoftwareDistribution\Download\*"
$initialUpdateSize = Get-DirectorySize "C:\Windows\SoftwareDistribution\Download"
Remove-Item $WindowsUpdateTemp -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialUpdateSize
Write-Host "Windows 更新临时文件已清理。"
# 删除系统日志文件
$LogFiles = "C:\Windows\Logs\*"
$initialLogSize = Get-DirectorySize "C:\Windows\Logs"
Remove-Item $LogFiles -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialLogSize
Write-Host "系统日志文件已清理。"
# 删除浏览器缓存(以 Chrome 和 Edge 为例)
$ChromeCache = "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache\*"
$initialChromeSize = Get-DirectorySize "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache"
Remove-Item $ChromeCache -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialChromeSize
Write-Host "Chrome 浏览器缓存已清理。"
$EdgeCache = "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache\*"
$initialEdgeSize = Get-DirectorySize "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache"
Remove-Item $EdgeCache -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialEdgeSize
Write-Host "Edge 浏览器缓存已清理。"
# 删除 Internet Explorer 临时文件
$IECache = "$env:LOCALAPPDATA\Microsoft\Windows\INetCache\*"
$initialIESize = Get-DirectorySize "$env:LOCALAPPDATA\Microsoft\Windows\INetCache"
Remove-Item $IECache -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialIESize
Write-Host "Internet Explorer 临时文件已清理。"
# 删除用户临时文件
$UserTemp = "C:\Users\$env:USERNAME\AppData\Local\Temp\*"
$initialUserTempSize = Get-DirectorySize "$UserTemp"
Remove-Item $UserTemp -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialUserTempSize
Write-Host "用户临时文件已清理。"
# 删除 Download 文件夹中超过30天未访问的文件
$DownloadFolder = "$env:USERPROFILE\Downloads\*"
Get-ChildItem -Path $DownloadFolder -File | Where-Object { $_.LastAccessTime -lt (Get-Date).AddDays(-30) } | ForEach-Object {
$totalFreedSpace += $_.Length
Remove-Item $_.FullName -Force
}
Write-Host "下载文件夹中超过30天未访问的文件已删除。"
# 删除最近使用文件列表
$RecentFolder = "$env:APPDATA\Microsoft\Windows\Recent\*"
$initialRecentSize = Get-DirectorySize "$env:APPDATA\Microsoft\Windows\Recent"
Remove-Item $RecentFolder -Recurse -Force -ErrorAction SilentlyContinue
$totalFreedSpace += $initialRecentSize
Write-Host "最近使用的文件列表已清理。"
# 删除特定类型的文件
$SystemDrive = $env:SystemDrive
$filesToDelete = @("$SystemDrive\*.tmp", "$SystemDrive\*._mp", "$SystemDrive\*.log", "$SystemDrive\*.gid", "$SystemDrive\*.chk", "$SystemDrive\*.old", "$SystemDrive\recycled\*.*", "$env:windir\*.bak", "$env:windir\prefetch\*.*")
foreach ($filePattern in $filesToDelete) {
$files = Get-ChildItem -Path $filePattern -ErrorAction SilentlyContinue
if ($files) {
Remove-Item $files.FullName -Force -ErrorAction SilentlyContinue
}
}
# 清空 Windows 临时文件夹
$WinTemp = "$env:windir\temp"
Remove-Item "$WinTemp\*" -Recurse -Force -ErrorAction SilentlyContinue
New-Item -ItemType Directory -Path $WinTemp -Force | Out-Null
# 完成清理
Write-Host "C 盘清理完成,总共释放空间: $([math]::round($totalFreedSpace / 1MB, 2)) MB。"
第二步:设置执行权限
1.打开命令提示符(CMD)
按 Win + R 键,输入 cmd,然后按 Enter。在命令提示符中,输入以下命令并按 Enter:powershell
2.打开一个新的 PowerShell 窗口
在这个窗口中,输入 Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
命令以允许脚本执行
3.确认是否生效
输入命令 Get-ExecutionPolicy -List
确保 CurrentUser 的策略是 RemoteSigned。现在,重新启动 PowerShell 就可以直接打开上面的脚本进行清理啦