forked from Myraas/Remote-Disk-Cleanup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoteDiskCleanup.ps1
553 lines (468 loc) · 21.2 KB
/
RemoteDiskCleanup.ps1
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# Name: RemoteDiskCleanup.ps1
# Creator: Myrianthi
# CreationDate: 11.26.2018
# LastModified: 5.26.2020
# Version: 2.5
# Doc: https://github.com/Myrianthi/remotediskcleanup
# Purpose: Remote-access temp file removal
# Requirements: Admin access, PS-Remoting enabled on remote devices
# I am a novice PoSh scripter. I use this often in production, but please use caution and run at your own risk.
# --------------------------- Script begins here --------------------------- #
#Requires -RunAsAdministrator
Set-ExecutionPolicy RemoteSigned
# This will check all Disk Cleanup boxes by manually setting each key in the following registry path to 2.
# Comment out the files that you do not want Disk Cleanup to erase.
$SageSet = "StateFlags0099"
$StateFlags= "Stateflags0099"
$Base = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\"
$VolCaches = Get-ChildItem "HKLM:\Software\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches"
$Locations = @(
"Active Setup Temp Folders"
"BranchCache"
"Downloaded Program Files"
"GameNewsFiles"
"GameStatisticsFiles"
"GameUpdateFiles"
"Internet Cache Files"
"Memory Dump Files"
"Offline Pages Files"
"Old ChkDsk Files"
"Previous Installations"
#"Recycle Bin"
"Service Pack Cleanup"
"Setup Log Files"
"System error memory dump files"
"System error minidump files"
"Temporary Files"
"Temporary Setup Files"
"Temporary Sync Files"
"Thumbnail Cache"
"Update Cleanup"
"Upgrade Discarded Files"
"User file versions"
"Windows Defender"
"Windows Error Reporting Archive Files"
"Windows Error Reporting Queue Files"
"Windows Error Reporting System Archive Files"
"Windows Error Reporting System Queue Files"
"Windows ESD installation files"
"Windows Upgrade Log Files"
)
Function Get-Recyclebin{
[CmdletBinding()]
Param
(
$ComputerOBJ,
$RetentionTime = "3"
)
Write-Host "Empyting Recycle bin items older than $RetentionTime days" -ForegroundColor Yellow
If($ComputerOBJ.PSRemoting -eq $true){
$Result = Invoke-Command -ComputerName $ComputerOBJ.ComputerName -ScriptBlock {
Try{
$Shell = New-Object -ComObject Shell.Application
$Recycler = $Shell.NameSpace(0xa)
$Recycler.Items()
foreach($item in $Recycler.Items())
{
$DeletedDate = $Recycler.GetDetailsOf($item,2) -replace "\u200f|\u200e","" #Invisible Unicode Characters
$DeletedDatetime = Get-Date $DeletedDate
[Int]$DeletedDays = (New-TimeSpan -Start $DeletedDatetime -End $(Get-Date)).Days
If($DeletedDays -ge $RetentionTime)
{
Remove-Item -Path $item.Path -Confirm:$false -Force -Recurse
}
}
}
Catch [System.Exception]{
$RecyclerError = $true
}
Finally{
If($RecyclerError -eq $False){
Write-output $True
}
Else{
Write-Output $False
}
}
} -Credential $ComputerOBJ.Credential
If($Result -eq $True){
Write-Host "All recycle bin items older than $RetentionTime days were deleted" -ForegroundColor Green
}
Else{
Write-Host "Unable to delete some items in the Recycle Bin." -ForegroundColor Red
}
}
Else{
Try{
$Shell = New-Object -ComObject Shell.Application
$Recycler = $Shell.NameSpace(0xa)
$Recycler.Items()
foreach($item in $Recycler.Items())
{
$DeletedDate = $Recycler.GetDetailsOf($item,2) -replace "\u200f|\u200e","" #Invisible Unicode Characters
$DeletedDatetime = Get-Date $DeletedDate
[Int]$DeletedDays = (New-TimeSpan -Start $DeletedDatetime -End $(Get-Date)).Days
If($DeletedDays -ge $RetentionTime)
{
Remove-Item -Path $item.Path -Confirm:$false -Force -Recurse
}
}
}
Catch [System.Exception]{
$RecyclerError = $true
}
Finally{
If($RecyclerError -eq $true){
Write-Host "Unable to delete some items in the Recycle Bin." -ForegroundColor Red
}
Else{
Write-Host "All recycle bin items older than $RetentionTime days were deleted" -ForegroundColor Green
}
}
}
}
Function Clean-Path{
Param
(
[String]$Path,
$ComputerOBJ
)
Write-Host "`t...Cleaning $Path"
If($ComputerOBJ.PSRemoting -eq $True){
Invoke-Command -ComputerName $ComputerOBJ.ComputerName -ScriptBlock {
If(Test-Path $Using:Path){
Foreach($Item in $(Get-ChildItem -Path $Using:Path -Recurse)){
Try{
Remove-item -Path $item.FullName -Confirm:$False -Recurse -ErrorAction Stop
}
Catch [System.Exception]{
Write-verbose "$($Item.path) - $($_.Exception.Message)"
}
}
}
} -Credential $ComputerOBJ.Credential
}
Else{
If(Test-Path $Path){
Foreach($Item in $(Get-ChildItem -Path $Path -Recurse)){
Try{
Remove-item -Path $item.FullName -Confirm:$False -Recurse -ErrorAction Stop
}
Catch [System.Exception]{
Write-verbose "$($Item.path) - $($_.Exception.Message)"
}
}
}
}
}
Function Get-OrigFreeSpace{
Param
(
$ComputerOBJ
)
Try{
$RawFreespace = (Get-WmiObject Win32_logicaldisk -ComputerName $ComputerOBJ.ComputerName -Credential $ComputerOBJ.Credential -ErrorAction Stop | Where-Object {$_.DeviceID -eq 'C:'}).freespace
$FreeSpaceGB = [decimal]("{0:N2}" -f($RawFreespace/1gb))
Write-host "Current Free Space on the OS Drive : $FreeSpaceGB GB" -ForegroundColor Magenta
}
Catch [System.Exception]{
$FreeSpaceGB = $False
Write-Host "Unable to pull free space from OS drive. Press enter to Exit..." -ForegroundColor Red
}
Finally{
$ComputerOBJ | Add-Member -MemberType NoteProperty -Name OrigFreeSpace -Value $FreeSpaceGB
Write-output $ComputerOBJ
}
}
Function Get-FinalFreeSpace{
Param
(
$ComputerOBJ
)
Try{
$RawFreespace = (Get-WmiObject Win32_logicaldisk -ComputerName $ComputerOBJ.ComputerName -Credential $ComputerOBJ.Credential -ErrorAction Stop | Where-Object {$_.DeviceID -eq 'C:'}).freespace
$FreeSpaceGB = [decimal]("{0:N2}" -f($RawFreespace/1gb))
Write-host "Final Free Space on the OS Drive : $FreeSpaceGB GB" -ForegroundColor Magenta
}
Catch [System.Exception]{
$FreeSpaceGB = $False
Write-Host "Unable to pull free space from OS drive. Press enter to Exit..." -ForegroundColor Red
}
Finally{
$ComputerOBJ | Add-Member -MemberType NoteProperty -Name FinalFreeSpace -Value $FreeSpaceGB
Write-output $ComputerOBJ
}
}
Function Get-Computername {
Write-Host "Please enter the computername to connect to or just hit enter for localhost" -ForegroundColor Yellow
$ComputerName = Read-Host
if($ComputerName -eq '' -or $ComputerName -eq $null){
$obj = New-object PSObject -Property @{
ComputerName = $env:COMPUTERNAME
Remote = $False
}
}
else{
$obj = New-object PSObject -Property @{
ComputerName = $Computername
Remote = $True
}
}
Write-output $obj
}
Function Test-PSRemoting{
Param
(
$ComputerOBJ
)
Write-Host "Please enter your credentials for the remote machine." -ForegroundColor Yellow
$ComputerOBJ | Add-Member NoteProperty -Name Credential -Value (Get-Credential)
$RemoteHostname = Invoke-command -ComputerName $ComputerOBJ.Computername -ScriptBlock {hostname} -Credential $ComputerOBJ.Credential -erroraction 'silentlycontinue'
If($RemoteHostname -eq $ComputerOBJ.Computername){
Write-Host "PowerShell Remoting was successful" -ForegroundColor Green
$ComputerOBJ | Add-Member NoteProperty -Name PSRemoting -Value $True
}
Else {
Write-host "PowerShell Remoting FAILED press enter to exit script." -ForegroundColor Red
$ComputerOBJ | Add-Member NoteProperty -Name PSRemoting -Value $False
}
Write-output $ComputerOBJ
}
Function Run-CleanMGR{
Param
(
$ComputerOBJ
)
If($ComputerOBJ.PSRemoting -eq $true){
Write-Host "Attempting to Run Windows Disk Cleanup With Parameters..." -ForegroundColor Yellow
Write-Host "`tApplying $sageset parameters to registry path:"
Write-Host "`t$Base"
$CleanMGR = Invoke-command -ComputerName $ComputerOBJ.ComputerName -ScriptBlock {
$ErrorActionPreference = 'Stop'
Try{
# Set Sageset99/Stateflag0099 reg keys to 1 for a blank slate.
foreach($VC in $VolCaches){
New-ItemProperty -Path "$($VC.PSPath)" -Name $StateFlags -Value 1 -Type DWORD -Force | Out-Null
}
ForEach($Location in $Locations) {
Set-ItemProperty -Path $($Base+$Location) -Name $SageSet -Type DWORD -Value 2 -ea silentlycontinue | Out-Null
}
# Convert the Sageset number previously defined and Run the Disk Cleanup process with configured parameters.
$Args = "/sagerun:$([string]([int]$SageSet.Substring($SageSet.Length-4)))"
Start-Process -Wait "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList $Args -WindowStyle Hidden
# Set Sageset99/Stateflag0099 reg keys back to 1 for a blank slate.
foreach($VC in $VolCaches){
New-ItemProperty -Path "$($VC.PSPath)" -Name $StateFlags -Value 1 -Type DWORD -Force | Out-Null
}
$ErrorActionPreference = 'SilentlyContinue'
#Write-Output $true
}
Catch [System.Exception]{
$ErrorActionPreference = 'SilentlyContinue'
#Write-output $False
}
} -Credential $ComputerOBJ.Credential
If($CleanMGR -eq $True){
ForEach($Location in $Locations) {
Write-Host "`t...Cleaning $Location"
}
Write-Host "Windows Disk Cleanup has been run successfully." -ForegroundColor Green
}
Else{
Write-host "Cleanmgr is not installed! To use this portion of the script you must install the following windows features:" -ForegroundColor Red
Write-host "Desktop-Experience, Ink-Handwriting" -ForegroundColor Red
}
}
Else{
Write-Host "Attempting to Run Windows Disk Cleanup With Parameters..." -ForegroundColor Yellow
Write-Host "`tApplying $sageset parameters to registry path:"
Write-Host "`t$Base"
Echo ""
$ErrorActionPreference = 'Stop'
Try{
# Set Sageset99/Stateflag0099 reg keys to 1 for a blank slate.
foreach($VC in $VolCaches){
New-ItemProperty -Path "$($VC.PSPath)" -Name $StateFlags -Value 1 -Type DWORD -Force | Out-Null
}
ForEach($Location in $Locations) {
Set-ItemProperty -Path $($Base+$Location) -Name $SageSet -Type DWORD -Value 2 -ea silentlycontinue | Out-Null
}
# Convert the Sageset number previously defined and Run the Disk Cleanup process with configured parameters.
$Args = "/sagerun:$([string]([int]$SageSet.Substring($SageSet.Length-4)))"
Start-Process -Wait "$env:SystemRoot\System32\cleanmgr.exe" -ArgumentList $Args -WindowStyle Hidden
# Set Sageset99/Stateflag0099 reg keys back to 1 for a blank slate.
foreach($VC in $VolCaches){
New-ItemProperty -Path "$($VC.PSPath)" -Name $StateFlags -Value 1 -Type DWORD -Force | Out-Null
}
$ErrorActionPreference = 'SilentlyContinue'
#Write-Output $true
ForEach($Location in $Locations) {
Write-Host "`t...Cleaning $Location"
}
Write-Host "Windows Disk Cleanup has been run successfully." -ForegroundColor Green
}
Catch [System.Exception]{
Write-host "Cleanmgr is not installed! To use this portion of the script you must install the following windows features:" -ForegroundColor Red
Write-host "Desktop-Experience, Ink-Handwriting" -ForegroundColor Red
}
$ErrorActionPreference = 'SilentlyContinue'
}
}
Function Erase-IExplorerHistory{
Param
(
$ComputerOBJ
)
If($ComputerOBJ.PSRemoting -eq $true){
Write-Host "Attempting to Erase Internet Explorer temp data" -ForegroundColor Yellow
$CleanIExplorer = Invoke-command -ComputerName $ComputerOBJ.ComputerName -ScriptBlock {
$ErrorActionPreference = 'Stop'
Try{
Start-Process -FilePath rundll32.exe -ArgumentList 'inetcpl.cpl,ClearMyTracksByProcess 4351' -Wait -NoNewWindow
$ErrorActionPreference = 'SilentlyContinue'
#Write-Output $true
}
Catch [System.Exception]{
$ErrorActionPreference = 'SilentlyContinue'
#Write-output $False
}
} -Credential $ComputerOBJ.Credential
If($CleanIExplorer -eq $True){
Write-Host "Internet Explorer temp data has been successfully erased" -ForegroundColor Green
}
Else{
Write-host "Failed to erase Internet Explorer temp data" -ForegroundColor Red
}
}
Else{
Write-Host "Attempting to Erase Internet Explorer temp data" -ForegroundColor Yellow
$ErrorActionPreference = 'Stop'
Try{
Start-Process -FilePath rundll32.exe -ArgumentList 'inetcpl.cpl,ClearMyTracksByProcess 4351' -Wait -NoNewWindow
Write-Host "Internet Explorer temp data has been successfully erased" -ForegroundColor Green
}
Catch [System.Exception]{
Write-host "Failed to erase Internet Explorer temp data" -ForegroundColor Red
}
$ErrorActionPreference = 'SilentlyContinue'
}
}
Function Run-DISM{
Param
(
$ComputerOBJ
)
If($ComputerOBJ.PSRemoting -eq $true){
Write-Host "Running DISM to clean old servicepack files" -ForegroundColor Yellow
$DISM = Invoke-command -ComputerName $ComputerOBJ.ComputerName -ScriptBlock {
$ErrorActionPreference = 'Stop'
Try{
$DISMResult = dism.exe /online /cleanup-Image /spsuperseded
$ErrorActionPreference = 'SilentlyContinue'
Write-Output $DISMResult
}
Catch [System.Exception]{
$ErrorActionPreference = 'SilentlyContinue'
Write-output $False
}
} -Credential $ComputerOBJ.Credential
If($DISM -match 'The operation completed successfully'){
Write-Host "DISM Completed Successfully." -ForegroundColor Green
}
Else{
Write-Host "Unable to clean old ServicePack Files." -ForegroundColor Red
}
}
Else{
Write-Host "Running DISM to clean old servicepack files" -ForegroundColor Yellow
$ErrorActionPreference = 'Stop'
Try{
$DISMResult = dism.exe /online /cleanup-Image /spsuperseded
$ErrorActionPreference = 'SilentlyContinue'
}
Catch [System.Exception]{
$ErrorActionPreference = 'SilentlyContinue'
$DISMResult = $False
}
$ErrorActionPreference = 'SilentlyContinue'
If($DISMResult -match 'The operation completed successfully'){
Write-Host "DISM Completed Successfully." -ForegroundColor Green
}
Else{
Write-Host "Unable to clean old ServicePack Files." -ForegroundColor Red
}
}
}
Clear-Host
Echo " **This tool will attempt to remove bloatware and erase temp files across all user profiles. Please use with caution.**"
Echo ""
$ComputerOBJ = Get-ComputerName
Echo "You have entered $ComputerOBJ. Is this correct?"
Pause
Echo ""
Start-Transcript -Path C:\Windows\System32\CleanupLogs\$ComputerOBJ.txt
[System.DateTime]::Now
Echo ""
Echo "********************************************************************************************************************"
If($ComputerOBJ.Remote -eq $true){
$ComputerOBJ = Test-PSRemoting -ComputerOBJ $ComputerOBJ
If($ComputerOBJ.PSRemoting -eq $False){
Read-Host
exit;
}
}
$ComputerOBJ = Get-OrigFreeSpace -ComputerOBJ $ComputerOBJ
If($ComputerOBJ.OrigFreeSpace -eq $False){
Read-host
exit;
}
Echo "********************************************************************************************************************"
Echo ""
#======================================================================================================================================================
Write-Host "Cleaning temp directories across all user profiles" -ForegroundColor Yellow
Clean-path -Path 'C:\Temp\*' -Verbose -ComputerOBJ $ComputerOBJ
Clean-path -Path 'C:\Windows\Temp\*' -Verbose -ComputerOBJ $ComputerOBJ
Clean-Path -Path 'C:\Users\*\Documents\*tmp' -Verbose -ComputerOBJ $ComputerOBJ
Clean-path -Path 'C:\Documents and Settings\*\Local Settings\Temp\*' -ComputerOBJ $ComputerOBJ
Clean-path -Path 'C:\Users\*\Appdata\Local\Temp\*' -Verbose -ComputerOBJ $ComputerOBJ
Clean-path -Path 'C:\Users\*\AppData\Local\Microsoft\Windows\Temporary Internet Files\*' -Verbose -ComputerOBJ $ComputerOBJ
Clean-path -Path 'C:\Users\*\AppData\Roaming\Microsoft\Windows\Cookies\*' -Verbose -ComputerOBJ $ComputerOBJ
## Optional paths. Clean at your own risk.
#Clean-path -Path 'C:\ServiceProfiles\LocalService\AppData\Local\Temp\*' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-path -Path 'C:\Windows\Prefetch' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-path -Path 'C:\Users\*\AppData\Local\Microsoft\Windows\INetCache'-Verbose -ComputerOBJ $ComputerOBJ
#Clean-Path -Path 'C:\Users\*\AppData\Roaming\Microsoft\Windows\Recent' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-Path -Path 'C:\AppData\Roaming\Microsoft\Windows\Recent' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-Path -Path 'C:\Users\*\AppData\Local\Google\Chrome\User Data\Default\Cache' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-Path -Path 'C:\Users\*\AppData\Local\Mozilla\Firefox\Profiles\*.default' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-Path -Path 'C:\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*.default' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-path -Path 'C:\ProgramData\Microsoft\Windows\WER\ReportArchive' -Verbose -ComputerOBJ $ComputerOBJ
#Clean-path -Path 'C:\ProgramData\Microsoft\Windows\WER\ReportQueue' -Verbose -ComputerOBJ $ComputerOBJ
Write-Host "All Temp Paths have been cleaned" -ForegroundColor Green
Echo ""
#======================================================================================================================================================
Run-CleanMGR -ComputerOBJ $ComputerOBJ
Echo ""
#Erase-IExplorerHistory -ComputerOBJ $ComputerOBJ
Echo ""
Run-DISM -ComputerOBJ $ComputerOBJ
Echo ""
Get-Recyclebin -ComputerOBJ $ComputerOBJ
Echo ""
Winmgmt /salvagerepository
Echo "********************************************************************************************************************"
$ComputerOBJ = Get-FinalFreeSpace -ComputerOBJ $ComputerOBJ
$SpaceRecovered = $($Computerobj.finalfreespace) - $($ComputerOBJ.OrigFreeSpace)
If($SpaceRecovered -lt 0){
Write-Host "Less than a Gigabyte of Free Space was Recovered." -ForegroundColor Magenta
}
ElseIf($SpaceRecovered -eq 0){
Write-host "No Space was Recovered" -ForegroundColor Magenta
}
Else{
Write-host "Free Space Recovered : $SpaceRecovered GB" -ForegroundColor Magenta
}
Echo "********************************************************************************************************************"
Echo ""
Stop-Transcript
# --------------------------- Script ends here --------------------------- #