-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpccleaner.ps1
More file actions
348 lines (294 loc) · 11.6 KB
/
pccleaner.ps1
File metadata and controls
348 lines (294 loc) · 11.6 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<#
.SYNOPSIS
Claude-Code-PCCleaner -- Full PC optimization for developers.
.DESCRIPTION
Orchestrates modular cleanup of disk space, RAM, package caches, build
artifacts, application caches, startup programs, and OneDrive. Operates
in three safety tiers: Scan (report only), Clean (interactive), and
Aggressive (auto-confirm maximum cleanup).
.PARAMETER Scan
Run all modules in report-only mode. This is the default behavior.
Nothing is modified on disk or in the registry.
.PARAMETER Clean
Run selected modules with interactive confirmations before each action.
.PARAMETER Aggressive
Run all modules with auto-confirmation for maximum cleanup.
.PARAMETER Caches
Target package manager caches (npm, uv, pip, Go, Cargo).
.PARAMETER Builds
Target build artifacts and stale dependencies.
.PARAMETER Processes
Target RAM analysis and process management.
.PARAMETER Apps
Target browser and Electron app caches.
.PARAMETER OneDrive
Target OneDrive local cache optimization.
.PARAMETER Startup
Target startup program audit.
.PARAMETER Verbose
Enable verbose output with additional detail.
.EXAMPLE
.\pccleaner.ps1 -Scan
Full system scan. Safe -- nothing is modified.
.EXAMPLE
.\pccleaner.ps1 -Clean -Caches
Clean package manager caches with confirmations.
.EXAMPLE
.\pccleaner.ps1 -Aggressive
Maximum cleanup across all modules, auto-confirmed.
#>
[CmdletBinding(DefaultParameterSetName = "ScanMode")]
param(
[Parameter(ParameterSetName = "ScanMode")]
[switch]$Scan,
[Parameter(ParameterSetName = "CleanMode")]
[switch]$Clean,
[Parameter(ParameterSetName = "AggressiveMode")]
[switch]$Aggressive,
[switch]$Caches,
[switch]$Builds,
[switch]$Processes,
[switch]$Apps,
[switch]$OneDrive,
[switch]$Startup
)
# ============================================================
# INITIALIZATION
# ============================================================
$ErrorActionPreference = "Continue"
$scriptRoot = $PSScriptRoot
# Determine mode
$mode = "Scan" # Default
if ($Clean) { $mode = "Clean" }
if ($Aggressive) { $mode = "Aggressive" }
# Determine which modules to run
$noModuleFlags = -not ($Caches -or $Builds -or $Processes -or $Apps -or $OneDrive -or $Startup)
$runAll = ($mode -eq "Scan" -and $noModuleFlags) -or ($mode -eq "Aggressive") -or $noModuleFlags
$runCaches = $runAll -or $Caches
$runBuilds = $runAll -or $Builds
$runProcesses = $runAll -or $Processes
$runApps = $runAll -or $Apps
$runOneDrive = $runAll -or $OneDrive
$runStartup = $runAll -or $Startup
# ============================================================
# LOAD CONFIG
# ============================================================
$configPath = Join-Path $scriptRoot "config\defaults.json"
if (-not (Test-Path $configPath)) {
Write-Host "ERROR: Config file not found at $configPath" -ForegroundColor Red
Write-Host "Expected: config\defaults.json relative to script location." -ForegroundColor Red
exit 1
}
try {
$configJson = Get-Content -Path $configPath -Raw | ConvertFrom-Json
# Convert PSCustomObject to hashtable for easier use in modules
$config = @{
dry_run = $configJson.dry_run
large_file_threshold_mb = $configJson.large_file_threshold_mb
inactive_project_days = $configJson.inactive_project_days
temp_age_days = $configJson.temp_age_days
exclusions = @{
directories = @($configJson.exclusions.directories)
files = @($configJson.exclusions.files)
}
cache_paths = @{
npm = $configJson.cache_paths.npm
uv = $configJson.cache_paths.uv
pip = $configJson.cache_paths.pip
go = $configJson.cache_paths.go
cargo = $configJson.cache_paths.cargo
}
build_patterns = @($configJson.build_patterns)
}
}
catch {
Write-Host "ERROR: Failed to parse config file: $_" -ForegroundColor Red
exit 1
}
# Check for local config override
$localConfigPath = Join-Path $scriptRoot "config\local.json"
if (Test-Path $localConfigPath) {
try {
$localJson = Get-Content -Path $localConfigPath -Raw | ConvertFrom-Json
# Override specific values from local config
if ($null -ne $localJson.large_file_threshold_mb) { $config.large_file_threshold_mb = $localJson.large_file_threshold_mb }
if ($null -ne $localJson.inactive_project_days) { $config.inactive_project_days = $localJson.inactive_project_days }
if ($null -ne $localJson.temp_age_days) { $config.temp_age_days = $localJson.temp_age_days }
Write-Host " Loaded local config override: $localConfigPath" -ForegroundColor DarkGray
}
catch {
Write-Host " Warning: Failed to parse local config, using defaults." -ForegroundColor Yellow
}
}
# ============================================================
# LOAD MODULES
# ============================================================
$modulesDir = Join-Path $scriptRoot "modules"
# Dot-source all module files
$moduleFiles = @(
"disk-audit.ps1",
"cache-cleaner.ps1",
"build-cleaner.ps1",
"process-audit.ps1",
"app-cleaner.ps1",
"startup-audit.ps1",
"onedrive-opt.ps1"
)
foreach ($moduleFile in $moduleFiles) {
$modulePath = Join-Path $modulesDir $moduleFile
if (Test-Path $modulePath) {
. $modulePath
}
else {
Write-Host " Warning: Module not found: $moduleFile" -ForegroundColor Yellow
}
}
# ============================================================
# BEFORE METRICS
# ============================================================
$beforeDisk = @{}
$drives = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Free -gt 0 }
foreach ($drive in $drives) {
$beforeDisk[$drive.Name] = $drive.Free
}
$beforeRAM = $null
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue
if ($osInfo) {
$beforeRAM = [long]$osInfo.FreePhysicalMemory * 1KB
}
$startTime = Get-Date
# ============================================================
# BANNER
# ============================================================
Write-Host ""
Write-Host " ================================================" -ForegroundColor Cyan
Write-Host " Claude-Code-PCCleaner" -ForegroundColor Cyan
Write-Host " Full PC Optimization for Developers" -ForegroundColor Cyan
Write-Host " ================================================" -ForegroundColor Cyan
Write-Host ""
$modeColor = switch ($mode) {
"Scan" { "Green" }
"Clean" { "Yellow" }
"Aggressive" { "Red" }
}
$modeLabel = switch ($mode) {
"Scan" { "SCAN (report only -- nothing will be modified)" }
"Clean" { "CLEAN (interactive -- will confirm before each action)" }
"Aggressive" { "AGGRESSIVE (auto-confirm -- maximum cleanup)" }
}
Write-Host " Mode: $modeLabel" -ForegroundColor $modeColor
Write-Host " Time: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')" -ForegroundColor DarkGray
Write-Host " User: $env:USERNAME @ $env:COMPUTERNAME" -ForegroundColor DarkGray
$activeModules = @()
if ($runCaches) { $activeModules += "Caches" }
if ($runBuilds) { $activeModules += "Builds" }
if ($runProcesses) { $activeModules += "Processes" }
if ($runApps) { $activeModules += "Apps" }
if ($runOneDrive) { $activeModules += "OneDrive" }
if ($runStartup) { $activeModules += "Startup" }
Write-Host " Modules: $($activeModules -join ', ')" -ForegroundColor DarkGray
Write-Host ""
# Safety warning for aggressive mode
if ($mode -eq "Aggressive") {
Write-Host " !! AGGRESSIVE MODE -- All actions will be auto-confirmed !!" -ForegroundColor Red
Write-Host ""
$confirm = Read-Host " Type 'YES' to proceed"
if ($confirm -ne "YES") {
Write-Host " Aborted." -ForegroundColor Yellow
exit 0
}
Write-Host ""
}
# ============================================================
# EXECUTE MODULES
# ============================================================
$totalFreed = [long]0
# Disk audit always runs first (information only)
if ($runAll) {
Invoke-DiskAudit -Mode $mode -Config $config
}
# Caches
if ($runCaches) {
$freed = Invoke-CacheCleaner -Mode $mode -Config $config
if ($freed) { $totalFreed += $freed }
}
# Build artifacts
if ($runBuilds) {
$freed = Invoke-BuildCleaner -Mode $mode -Config $config
if ($freed) { $totalFreed += $freed }
}
# Process audit
if ($runProcesses) {
Invoke-ProcessAudit -Mode $mode -Config $config
}
# Application caches
if ($runApps) {
$freed = Invoke-AppCleaner -Mode $mode -Config $config
if ($freed) { $totalFreed += $freed }
}
# Startup audit (Windows only)
if ($runStartup) {
Invoke-StartupAudit -Mode $mode -Config $config
}
# OneDrive optimization (Windows only)
if ($runOneDrive) {
$freed = Invoke-OneDriveOpt -Mode $mode -Config $config
if ($freed) { $totalFreed += $freed }
}
# ============================================================
# AFTER METRICS & SUMMARY
# ============================================================
$endTime = Get-Date
$elapsed = $endTime - $startTime
Write-Host ""
Write-Host "========================================" -ForegroundColor Cyan
Write-Host " SUMMARY" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host " Mode: $mode" -ForegroundColor $modeColor
Write-Host " Duration: $([math]::Round($elapsed.TotalSeconds, 1)) seconds" -ForegroundColor DarkGray
Write-Host " Modules: $($activeModules -join ', ')" -ForegroundColor DarkGray
Write-Host ""
# Disk delta
if ($mode -ne "Scan") {
Write-Host "[Disk Space Delta]" -ForegroundColor Yellow
Write-Host ""
$drivesAfter = Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Free -gt 0 }
foreach ($drive in $drivesAfter) {
if ($beforeDisk.ContainsKey($drive.Name)) {
$delta = $drive.Free - $beforeDisk[$drive.Name]
$deltaSign = if ($delta -ge 0) { "+" } else { "" }
$deltaColor = if ($delta -gt 0) { "Green" } elseif ($delta -lt 0) { "Red" } else { "DarkGray" }
Write-Host " $($drive.Name):\ Before: $(Format-Size $beforeDisk[$drive.Name]) After: $(Format-Size $drive.Free) Delta: $deltaSign$(Format-Size ([math]::Abs($delta)))" -ForegroundColor $deltaColor
}
}
Write-Host ""
# RAM delta
$osInfoAfter = Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction SilentlyContinue
if ($osInfoAfter -and $beforeRAM) {
$afterRAM = [long]$osInfoAfter.FreePhysicalMemory * 1KB
$ramDelta = $afterRAM - $beforeRAM
$ramSign = if ($ramDelta -ge 0) { "+" } else { "" }
$ramColor = if ($ramDelta -gt 0) { "Green" } elseif ($ramDelta -lt 0) { "Red" } else { "DarkGray" }
Write-Host "[RAM Delta]" -ForegroundColor Yellow
Write-Host ""
Write-Host " Before: $(Format-Size $beforeRAM) After: $(Format-Size $afterRAM) Delta: $ramSign$(Format-Size ([math]::Abs($ramDelta)))" -ForegroundColor $ramColor
Write-Host ""
}
# Total freed
if ($totalFreed -gt 0) {
Write-Host "[Total Space Freed]" -ForegroundColor Green
Write-Host ""
Write-Host " $(Format-Size $totalFreed)" -ForegroundColor Green
Write-Host ""
}
}
else {
Write-Host " No changes made (scan mode)." -ForegroundColor Green
Write-Host " Run with -Clean or -Aggressive to take action." -ForegroundColor DarkGray
Write-Host ""
}
Write-Host " ================================================" -ForegroundColor Cyan
Write-Host " Scan complete." -ForegroundColor Cyan
Write-Host " ================================================" -ForegroundColor Cyan
Write-Host ""