diff --git a/PCRemediation/Invoke-PCRemediation.ps1 b/PCRemediation/Invoke-PCRemediation.ps1 index e8cd94a..7bd5a55 100644 --- a/PCRemediation/Invoke-PCRemediation.ps1 +++ b/PCRemediation/Invoke-PCRemediation.ps1 @@ -6,7 +6,8 @@ PC Remediation & Cleanup - Remote Edition .DESCRIPTION This script provides targeted manual remediation tools to free up disk space, restart stopped - critical services, clear update caches, purge temporary logs, and handle pending reboot flags. + critical services, clear update caches, purge temporary logs, handle pending reboot flags, + clean browser caches, and report disk space changes. Compatible with Windows PowerShell 5.1 and supports local/remote CIM sessions (DCOM and WinRM). @@ -215,7 +216,8 @@ function Show-Menu { Write-Host "6. Empty Recycle Bin (All Users)" -ForegroundColor White Write-Host "7. Run Component Store Cleanup (DISM)" -ForegroundColor White Write-Host "8. Run Disk Cleanup (cleanmgr)" -ForegroundColor White - Write-Host "9. Execute Full System Remediation (All Items)" -ForegroundColor Green + Write-Host "9. Clear Browser Caches" -ForegroundColor White + Write-Host "10. Execute Full System Remediation (All Items)" -ForegroundColor Green Write-Host "Q. Quit" -ForegroundColor Red } } @@ -507,6 +509,136 @@ Start-Process -FilePath "cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait -WindowS } } +function Get-DiskSpaceInfo { + param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null) + + if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) { + $disks = Get-PSDrive -PSProvider FileSystem | + Select-Object Name, + @{Name="FreeSpace(GB)";Expression={[math]::Round($_.Free/1GB, 2)}}, + @{Name="TotalSpace(GB)";Expression={[math]::Round(($_.Used/1GB + $_.Free/1GB), 2)}}, + @{Name="PercentFree";Expression={[math]::Round(($_.Free/($_.Used + $_.Free))*100, 2)}} + } else { + $disks = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DriveType=3" -CimSession $CimSession | + Select-Object DeviceID, + @{Name="FreeSpace(GB)";Expression={[math]::Round($_.FreeSpace/1GB, 2)}}, + @{Name="TotalSpace(GB)";Expression={[math]::Round(($_.FreeSpace/1GB + $_.Size/1GB), 2)}}, + @{Name="PercentFree";Expression={[math]::Round(($_.FreeSpace/($_.FreeSpace + $_.Size))*100, 2)}} + } + + return $disks +} + +function Clear-WindowsUpdateFiles { + param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null) + Write-ColorOutput "`nCleaning Windows Update Files..." "Yellow" "Info" $TargetComputer + + try { + $beforeSpace = Get-DiskSpaceInfo -TargetComputer $TargetComputer -CimSession $CimSession + + if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) { + Stop-Service -Name "wuauserv", "bits" -Force -ErrorAction SilentlyContinue + + $updatePaths = @( + "$env:SystemRoot\SoftwareDistribution\Download\*", + "$env:SystemRoot\SoftwareDistribution\DataStore\*", + "$env:SystemRoot\SoftwareDistribution\Backup\*" + ) + foreach ($path in $updatePaths) { + Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue + } + + $oldUpdates = "$env:SystemRoot\WinSxS\Backup\*" + if (Test-Path $oldUpdates) { + Remove-Item -Path $oldUpdates -Recurse -Force -ErrorAction SilentlyContinue + } + + Start-Service -Name "wuauserv", "bits" -ErrorAction SilentlyContinue + } else { + $scriptBlock = @" +Stop-Service -Name 'wuauserv', 'bits' -Force -ErrorAction SilentlyContinue +`$updatePaths = @('$env:SystemRoot\SoftwareDistribution\Download\*', '$env:SystemRoot\SoftwareDistribution\DataStore\*', '$env:SystemRoot\SoftwareDistribution\Backup\*') +foreach (`$path in `$updatePaths) { + Remove-Item -Path `$path -Recurse -Force -ErrorAction SilentlyContinue +} +`$oldUpdates = '$env:SystemRoot\WinSxS\Backup\*' +if (Test-Path `$oldUpdates) { + Remove-Item -Path `$oldUpdates -Recurse -Force -ErrorAction SilentlyContinue +} +Start-Service -Name 'wuauserv', 'bits' -ErrorAction SilentlyContinue +"@ + Invoke-CimMethod -CimSession $CimSession -ClassName Win32_Process -MethodName Create -Arguments @{ + CommandLine = "powershell.exe -Command `$scriptBlock" + } | Out-Null + } + + $afterSpace = Get-DiskSpaceInfo -TargetComputer $TargetComputer -CimSession $CimSession + + foreach ($disk in $beforeSpace) { + $before = $disk.'FreeSpace(GB)' + $after = ($afterSpace | Where-Object { $_.Name -eq $disk.Name }).'FreeSpace(GB)' + $freed = [math]::Round($after - $before, 2) + Write-ColorOutput "Drive $($disk.Name): Freed $freed GB (was $before GB, now $after GB free)" "Green" "Good" $TargetComputer + } + + Write-ColorOutput "Windows Update files cleaned successfully." "Green" "Good" $TargetComputer + } catch { + Write-ColorOutput "Failed to clean Windows Update files: $_" "Red" "Critical" $TargetComputer + } +} + +function Clear-BrowserCaches { + param ([string]$TargetComputer = $env:COMPUTERNAME, [Microsoft.Management.Infrastructure.CimSession]$CimSession = $null) + Write-ColorOutput "`nClearing Browser Caches..." "Yellow" "Info" $TargetComputer + + try { + if (-not $CimSession -and $TargetComputer -eq $env:COMPUTERNAME) { + $userProfiles = Get-ChildItem -Path "C:\Users" -Directory -ErrorAction SilentlyContinue + foreach ($profile in $userProfiles) { + $username = $profile.Name + + $chromeCache = "$($profile.FullName)\AppData\Local\Google\Chrome\User Data\Default\Cache\*" + $chromeCodeCache = "$($profile.FullName)\AppData\Local\Google\Chrome\User Data\Default\Code Cache\*" + $firefoxCache = "$($profile.FullName)\AppData\Local\Mozilla\Firefox\Profiles\*\cache*" + $edgeCache = "$($profile.FullName)\AppData\Local\Microsoft\Edge\User Data\Default\Cache\*" + + foreach ($path in @($chromeCache, $chromeCodeCache, $firefoxCache, $edgeCache)) { + if (Test-Path $path) { + Remove-Item -Path $path -Recurse -Force -ErrorAction SilentlyContinue + } + } + + Write-ColorOutput " Cleared browser caches for user: $username" "White" "Info" $TargetComputer + } + } else { + $scriptBlock = @" +`$userProfiles = Get-ChildItem -Path 'C:\Users' -Directory -ErrorAction SilentlyContinue +foreach (`$profile in `$userProfiles) { + `$username = `$profile.Name + `$chromeCache = "`$(`$profile.FullName)\AppData\Local\Google\Chrome\User Data\Default\Cache\*" + `$chromeCodeCache = "`$(`$profile.FullName)\AppData\Local\Google\Chrome\User Data\Default\Code Cache\*" + `$firefoxCache = "`$(`$profile.FullName)\AppData\Local\Mozilla\Firefox\Profiles\*\cache*" + `$edgeCache = "`$(`$profile.FullName)\AppData\Local\Microsoft\Edge\User Data\Default\Cache\*" + + foreach (`$path in @(`$chromeCache, `$chromeCodeCache, `$firefoxCache, `$edgeCache)) { + if (Test-Path `$path) { + Remove-Item -Path `$path -Recurse -Force -ErrorAction SilentlyContinue + } + } + Write-Host " Cleared browser caches for user: `$username" -ForegroundColor White +} +"@ + Invoke-CimMethod -CimSession $CimSession -ClassName Win32_Process -MethodName Create -Arguments @{ + CommandLine = "powershell.exe -Command `$scriptBlock" + } | Out-Null + } + + Write-ColorOutput "Browser caches cleared successfully." "Green" "Good" $TargetComputer + } catch { + Write-ColorOutput "Failed to clear browser caches: $_" "Red" "Critical" $TargetComputer + } +} + function Run-AllRemediations { param ([string]$TargetComputer = $env:COMPUTERNAME) @@ -519,6 +651,7 @@ function Run-AllRemediations { try { Clear-WindowsUpdateCache -TargetComputer $TargetComputer -CimSession $cimSession + Clear-WindowsUpdateFiles -TargetComputer $TargetComputer -CimSession $cimSession Clear-TempFiles -TargetComputer $TargetComputer -CimSession $cimSession -MaxAgeDays 3 Clear-WerDumps -TargetComputer $TargetComputer -CimSession $cimSession Reset-NetworkStack -TargetComputer $TargetComputer -CimSession $cimSession @@ -526,6 +659,7 @@ function Run-AllRemediations { Clear-RecycleBinFiles -TargetComputer $TargetComputer -CimSession $cimSession Invoke-DismCleanup -TargetComputer $TargetComputer -CimSession $cimSession Invoke-CleanMgrCleanup -TargetComputer $TargetComputer -CimSession $cimSession + Clear-BrowserCaches -TargetComputer $TargetComputer -CimSession $cimSession Write-ColorOutput "`nRemediation Cycle Completed!" "Cyan" "Good" $TargetComputer } finally { @@ -564,7 +698,8 @@ while (-not $exitRequested) { "6" { Clear-RecycleBinFiles } "7" { Invoke-DismCleanup } "8" { Invoke-CleanMgrCleanup } - "9" { Run-AllRemediations } + "9" { Clear-BrowserCaches } + "10" { Run-AllRemediations } "Q" { Write-ColorOutput "Exiting script..." "Cyan"; $exitRequested = $true } default { Write-ColorOutput "Invalid selection." "Red" "Critical" } }