-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_all_cache.ps1
More file actions
231 lines (193 loc) · 10.4 KB
/
Copy pathclear_all_cache.ps1
File metadata and controls
231 lines (193 loc) · 10.4 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Clear All Cache - Windows System
# Run this script as Administrator
# Check if running as Administrator
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) {
Write-Host "This script requires Administrator privileges. Please run as Administrator." -ForegroundColor Red
Write-Host "Right-click on PowerShell and select 'Run as Administrator'" -ForegroundColor Yellow
pause
exit
}
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " Clear All Cache - Windows" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "This script will clear all types of cache:" -ForegroundColor White
Write-Host "- Windows Update Cache" -ForegroundColor White
Write-Host "- DNS Cache" -ForegroundColor White
Write-Host "- Temporary Files" -ForegroundColor White
Write-Host "- Browser Cache (Chrome, Firefox, Edge)" -ForegroundColor White
Write-Host "- Windows Store Cache" -ForegroundColor White
Write-Host "- System Cache" -ForegroundColor White
Write-Host "- User Profile Cache" -ForegroundColor White
Write-Host "- Print Spooler Cache" -ForegroundColor White
Write-Host "- Font Cache" -ForegroundColor White
Write-Host ""
$confirm = Read-Host "Do you want to continue? (y/N)"
if ($confirm -ne "y" -and $confirm -ne "Y") {
Write-Host "Operation cancelled." -ForegroundColor Yellow
pause
exit
}
Write-Host ""
Write-Host "Starting cache cleanup process..." -ForegroundColor Yellow
Write-Host ""
# Function to clear directory safely
function Clear-Directory {
param([string]$Path, [string]$Description)
if (Test-Path $Path) {
try {
Remove-Item -Path $Path -Recurse -Force -ErrorAction Stop
Write-Host "✓ $Description cleared" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to clear $Description : $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host "- $Description not found" -ForegroundColor Gray
}
}
# Function to clear files safely
function Clear-Files {
param([string]$Path, [string]$Pattern, [string]$Description)
if (Test-Path $Path) {
try {
Get-ChildItem -Path $Path -Filter $Pattern -Recurse | Remove-Item -Force -ErrorAction Stop
Write-Host "✓ $Description cleared" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to clear $Description : $($_.Exception.Message)" -ForegroundColor Red
}
} else {
Write-Host "- $Description not found" -ForegroundColor Gray
}
}
try {
Write-Host "========================================" -ForegroundColor Yellow
Write-Host "Clearing Windows Update Cache..." -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
# Stop Windows Update services
$services = @("wuauserv", "bits", "cryptsvc")
foreach ($service in $services) {
try {
Stop-Service -Name $service -Force -ErrorAction Stop
Write-Host "✓ Stopped $service service" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to stop $service service" -ForegroundColor Red
}
}
# Clear Windows Update cache
Clear-Directory -Path "$env:SystemRoot\SoftwareDistribution\Download" -Description "Windows Update cache"
Clear-Files -Path "$env:SystemRoot\SoftwareDistribution" -Pattern "*.log" -Description "Windows Update logs"
# Restart Windows Update services
foreach ($service in $services) {
try {
Start-Service -Name $service -ErrorAction Stop
Write-Host "✓ Started $service service" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to start $service service" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Yellow
Write-Host "Clearing DNS Cache..." -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
try {
Clear-DnsClientCache
Write-Host "✓ DNS cache flushed" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to flush DNS cache" -ForegroundColor Red
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Yellow
Write-Host "Clearing Temporary Files..." -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
Clear-Directory -Path $env:TEMP -Description "User temp files"
Clear-Directory -Path "$env:SystemRoot\Temp" -Description "System temp files"
# Recreate temp directories
New-Item -ItemType Directory -Path $env:TEMP -Force | Out-Null
New-Item -ItemType Directory -Path "$env:SystemRoot\Temp" -Force | Out-Null
Write-Host ""
Write-Host "========================================" -ForegroundColor Yellow
Write-Host "Clearing Windows Store Cache..." -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
Get-ChildItem -Path "$env:LOCALAPPDATA\Packages" -Filter "Microsoft.WindowsStore_*" -Directory | ForEach-Object {
Clear-Directory -Path "$($_.FullName)\LocalCache" -Description "Windows Store cache"
Clear-Directory -Path "$($_.FullName)\LocalState" -Description "Windows Store state"
}
Write-Host ""
Write-Host "========================================" -ForegroundColor Yellow
Write-Host "Clearing System Cache..." -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Yellow
Clear-Directory -Path "$env:SystemRoot\Prefetch" -Description "Prefetch cache"
New-Item -ItemType Directory -Path "$env:SystemRoot\Prefetch" -Force | Out-Null
Clear-Files -Path "$env:SystemRoot\System32\winevt\Logs" -Pattern "*.evtx" -Description "Event logs"
Write-Host ""
Write-Host "=======================================" -ForegroundColor Yellow
Write-Host "Clearing Browser Cache..." -ForegroundColor Yellow
Write-Host "=======================================" -ForegroundColor Yellow
# Chrome Cache
Clear-Directory -Path "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Cache" -Description "Chrome cache"
Clear-Directory -Path "$env:LOCALAPPDATA\Google\Chrome\User Data\Default\Code Cache" -Description "Chrome code cache"
# Firefox Cache
if (Test-Path "$env:APPDATA\Mozilla\Firefox\Profiles") {
Get-ChildItem -Path "$env:APPDATA\Mozilla\Firefox\Profiles" -Directory | ForEach-Object {
Clear-Directory -Path "$($_.FullName)\cache2" -Description "Firefox cache"
Clear-Directory -Path "$($_.FullName)\cache" -Description "Firefox cache"
}
Write-Host "✓ Firefox cache cleared" -ForegroundColor Green
}
# Edge Cache
Clear-Directory -Path "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Cache" -Description "Edge cache"
Clear-Directory -Path "$env:LOCALAPPDATA\Microsoft\Edge\User Data\Default\Code Cache" -Description "Edge code cache"
Write-Host ""
Write-Host "=======================================" -ForegroundColor Yellow
Write-Host "Clearing User Profile Cache..." -ForegroundColor Yellow
Write-Host "=======================================" -ForegroundColor Yellow
Clear-Directory -Path "$env:APPDATA\Microsoft\Windows\Recent" -Description "Recent files cache"
New-Item -ItemType Directory -Path "$env:APPDATA\Microsoft\Windows\Recent" -Force | Out-Null
Clear-Files -Path "$env:APPDATA\Microsoft\Windows\Explorer" -Pattern "thumbcache_*.db" -Description "Thumbnail cache"
Write-Host ""
Write-Host "=======================================" -ForegroundColor Yellow
Write-Host "Clearing Print Spooler Cache..." -ForegroundColor Yellow
Write-Host "=======================================" -ForegroundColor Yellow
try {
Stop-Service -Name "Spooler" -Force -ErrorAction Stop
Clear-Directory -Path "$env:SystemRoot\System32\spool\PRINTERS" -Description "Print spooler cache"
Start-Service -Name "Spooler" -ErrorAction Stop
Write-Host "✓ Print spooler cache cleared" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to clear print spooler cache" -ForegroundColor Red
}
Write-Host ""
Write-Host "=======================================" -ForegroundColor Yellow
Write-Host "Clearing Font Cache..." -ForegroundColor Yellow
Write-Host "=======================================" -ForegroundColor Yellow
try {
Stop-Service -Name "FontCache" -Force -ErrorAction Stop
Clear-Directory -Path "$env:LOCALAPPDATA\Microsoft\Windows\FontCache" -Description "Font cache"
Start-Service -Name "FontCache" -ErrorAction Stop
Write-Host "✓ Font cache cleared" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to clear font cache" -ForegroundColor Red
}
Write-Host ""
Write-Host "=======================================" -ForegroundColor Yellow
Write-Host "Running Disk Cleanup..." -ForegroundColor Yellow
Write-Host "=======================================" -ForegroundColor Yellow
try {
Start-Process -FilePath "cleanmgr" -ArgumentList "/sagerun:1" -Wait -WindowStyle Hidden
Write-Host "✓ Disk cleanup completed" -ForegroundColor Green
} catch {
Write-Host "✗ Failed to run disk cleanup" -ForegroundColor Red
}
Write-Host ""
Write-Host "=======================================" -ForegroundColor Green
Write-Host "Cache Cleanup Complete!" -ForegroundColor Green
Write-Host "=======================================" -ForegroundColor Green
Write-Host ""
Write-Host "All cache has been cleared successfully." -ForegroundColor White
Write-Host "You may need to restart your computer for all changes to take effect." -ForegroundColor Yellow
Write-Host ""
} catch {
Write-Host "Error occurred: $($_.Exception.Message)" -ForegroundColor Red
}
Write-Host "Press any key to exit..."
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")