Add Invoke-CleanMgrCleanup function
- Runs cleanmgr with all 20 standard cleanup items enabled - Uses StateFlags0001 registry values for persistent configuration - Supports both local and remote CIM sessions - Updated menu to include option 8 for Disk Cleanup
This commit is contained in:
1 parent
990b147c47
commit
731227dabe
1 file changed
+88
-2
@@ -214,7 +214,8 @@ function Show-Menu {
|
||||
Write-Host "5. Restart Automatic Services That Are Stopped" -ForegroundColor White
|
||||
Write-Host "6. Empty Recycle Bin (All Users)" -ForegroundColor White
|
||||
Write-Host "7. Run Component Store Cleanup (DISM)" -ForegroundColor White
|
||||
Write-Host "8. Execute Full System Remediation (All Items)" -ForegroundColor Green
|
||||
Write-Host "8. Run Disk Cleanup (cleanmgr)" -ForegroundColor White
|
||||
Write-Host "9. Execute Full System Remediation (All Items)" -ForegroundColor Green
|
||||
Write-Host "Q. Quit" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
@@ -423,6 +424,89 @@ function Invoke-DismCleanup {
|
||||
}
|
||||
}
|
||||
|
||||
function Invoke-CleanMgrCleanup {
|
||||
param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null)
|
||||
Write-ColorOutput "`nRunning Disk Cleanup (cleanmgr)..." "Yellow" "Info" $TargetComputer
|
||||
|
||||
try {
|
||||
$drive = $env:SystemDrive -replace ':', ''
|
||||
$regPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$drive"
|
||||
|
||||
if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) {
|
||||
$cleanupItems = @(
|
||||
"Active Setup Temp Folder",
|
||||
"BranchCache",
|
||||
"Downloaded Program Files",
|
||||
"GameNewsFiles",
|
||||
"Internet Cache Files",
|
||||
"Memory Dump Files",
|
||||
"Old ChkDsk Files",
|
||||
"Previous Installations",
|
||||
"Recycle Bin",
|
||||
"Setup Log Files",
|
||||
"System error memory dump files",
|
||||
"System error minidump files",
|
||||
"Temporary Files",
|
||||
"Temporary Setup Files",
|
||||
"Temporary Sync Files",
|
||||
"Windows Error Reporting Archive Files",
|
||||
"Windows Error Reporting Queue Files",
|
||||
"Windows Error Reporting System Archive Files",
|
||||
"Windows Error Reporting System Queue Files",
|
||||
"Windows Update Cleanup"
|
||||
)
|
||||
|
||||
foreach ($item in $cleanupItems) {
|
||||
$itemPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$item"
|
||||
if (Test-Path $itemPath) {
|
||||
New-ItemProperty -Path $itemPath -Name "StateFlags0001" -Value 2 -PropertyType DWord -Force | Out-Null
|
||||
}
|
||||
}
|
||||
|
||||
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait -WindowStyle Normal
|
||||
} else {
|
||||
$scriptBlock = @"
|
||||
`$drive = `$env:SystemDrive -replace ':', ''
|
||||
`$cleanupItems = @(
|
||||
"Active Setup Temp Folder",
|
||||
"BranchCache",
|
||||
"Downloaded Program Files",
|
||||
"GameNewsFiles",
|
||||
"Internet Cache Files",
|
||||
"Memory Dump Files",
|
||||
"Old ChkDsk Files",
|
||||
"Previous Installations",
|
||||
"Recycle Bin",
|
||||
"Setup Log Files",
|
||||
"System error memory dump files",
|
||||
"System error minidump files",
|
||||
"Temporary Files",
|
||||
"Temporary Setup Files",
|
||||
"Temporary Sync Files",
|
||||
"Windows Error Reporting Archive Files",
|
||||
"Windows Error Reporting Queue Files",
|
||||
"Windows Error Reporting System Archive Files",
|
||||
"Windows Error Reporting System Queue Files",
|
||||
"Windows Update Cleanup"
|
||||
)
|
||||
foreach (`$item in `$cleanupItems) {
|
||||
`$itemPath = "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\$item"
|
||||
if (Test-Path `$itemPath) {
|
||||
New-ItemProperty -Path `$itemPath -Name "StateFlags0001" -Value 2 -PropertyType DWord -Force | Out-Null
|
||||
}
|
||||
}
|
||||
Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait -WindowStyle Normal
|
||||
"@
|
||||
Invoke-CimMethod -CimSession $CimSession -ClassName Win32_Process -MethodName Create -Arguments @{
|
||||
CommandLine = "powershell.exe -Command `$scriptBlock"
|
||||
} | Out-Null
|
||||
}
|
||||
Write-ColorOutput "Disk Cleanup completed." "Green" "Good" $TargetComputer
|
||||
} catch {
|
||||
Write-ColorOutput "Failed to execute Disk Cleanup: $_" "Red" "Critical" $TargetComputer
|
||||
}
|
||||
}
|
||||
|
||||
function Run-AllRemediations {
|
||||
param ([string]$TargetComputer = $env:COMPUTERNAME)
|
||||
|
||||
@@ -441,6 +525,7 @@ function Run-AllRemediations {
|
||||
Repair-StoppedAutomaticServices -TargetComputer $TargetComputer -CimSession $cimSession
|
||||
Clear-RecycleBinFiles -TargetComputer $TargetComputer -CimSession $cimSession
|
||||
Invoke-DismCleanup -TargetComputer $TargetComputer -CimSession $cimSession
|
||||
Invoke-CleanMgrCleanup -TargetComputer $TargetComputer -CimSession $cimSession
|
||||
|
||||
Write-ColorOutput "`nRemediation Cycle Completed!" "Cyan" "Good" $TargetComputer
|
||||
} finally {
|
||||
@@ -478,7 +563,8 @@ while (-not $exitRequested) {
|
||||
"5" { Repair-StoppedAutomaticServices }
|
||||
"6" { Clear-RecycleBinFiles }
|
||||
"7" { Invoke-DismCleanup }
|
||||
"8" { Run-AllRemediations }
|
||||
"8" { Invoke-CleanMgrCleanup }
|
||||
"9" { Run-AllRemediations }
|
||||
"Q" { Write-ColorOutput "Exiting script..." "Cyan"; $exitRequested = $true }
|
||||
default { Write-ColorOutput "Invalid selection." "Red" "Critical" }
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user