# PCS Toolkit - WSUS/Windows Update Cleanup $timestamp = Get-Date -Format "yyyy-MM-dd_HH-mm-ss" $logFile = "$env:USERPROFILE\Desktop\WSUSCleanup_$timestamp.txt" function Log($msg) { Write-Host $msg Add-Content $logFile $msg } function LogColor($msg, $color) { Write-Host $msg -ForegroundColor $color Add-Content $logFile $msg } function Get-FolderSize($path) { if (Test-Path $path) { $size = (Get-ChildItem $path -Recurse -Force -EA SilentlyContinue | Measure-Object -Property Length -Sum).Sum return [math]::Round($size / 1MB, 2) } return 0 } Log "========================================" Log " PCS Toolkit - WSUS Cleanup" Log "========================================" Log "Generated: $(Get-Date)" Log "Computer: $env:COMPUTERNAME" Log "" $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { LogColor "ERROR: This script must be run as Administrator" "Red" Read-Host "Press Enter to exit" exit } $totalFreed = 0 Log "=== PRE-CLEANUP SIZES ===" Log "" $wuSize = Get-FolderSize "$env:SystemRoot\SoftwareDistribution" $cbsSize = Get-FolderSize "$env:SystemRoot\Logs\CBS" Log "SoftwareDistribution: $wuSize MB" Log "CBS Logs: $cbsSize MB" Log "" Log "=== STOPPING SERVICES ===" Log "" $services = @('wuauserv', 'bits', 'cryptsvc', 'msiserver') foreach ($svc in $services) { Log "Stopping $svc..." Stop-Service -Name $svc -Force -EA SilentlyContinue } Start-Sleep -Seconds 3 Log "" Log "=== CLEANING WINDOWS UPDATE CACHE ===" Log "" $downloadPath = "$env:SystemRoot\SoftwareDistribution\Download" if (Test-Path $downloadPath) { $beforeSize = Get-FolderSize $downloadPath Remove-Item "$downloadPath\*" -Recurse -Force -EA SilentlyContinue $afterSize = Get-FolderSize $downloadPath $freed = $beforeSize - $afterSize $totalFreed += $freed LogColor "Cleared Download folder: $freed MB freed" "Green" } $dataStorePath = "$env:SystemRoot\SoftwareDistribution\DataStore" if (Test-Path $dataStorePath) { $beforeSize = Get-FolderSize $dataStorePath Remove-Item "$dataStorePath\*" -Recurse -Force -EA SilentlyContinue $afterSize = Get-FolderSize $dataStorePath $freed = $beforeSize - $afterSize $totalFreed += $freed LogColor "Cleared DataStore: $freed MB freed" "Green" } Log "" Log "=== CLEANING CBS LOGS ===" Log "" $cbsPath = "$env:SystemRoot\Logs\CBS" if (Test-Path $cbsPath) { $beforeSize = Get-FolderSize $cbsPath Get-ChildItem $cbsPath -Filter "*.log" | Where-Object { $_.LastWriteTime -lt (Get-Date).AddDays(-7) } | Remove-Item -Force -EA SilentlyContinue $afterSize = Get-FolderSize $cbsPath $freed = $beforeSize - $afterSize $totalFreed += $freed LogColor "Cleaned CBS logs (older than 7 days): $freed MB freed" "Green" } Log "" Log "=== RUNNING DISM CLEANUP ===" Log "" Log "Removing superseded components (this may take several minutes)..." try { $dismResult = Dism.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase 2>&1 Log $dismResult LogColor "DISM cleanup completed" "Green" } catch { LogColor "DISM cleanup error: $_" "Yellow" } Log "" Log "=== RESTARTING SERVICES ===" Log "" foreach ($svc in $services) { Log "Starting $svc..." Start-Service -Name $svc -EA SilentlyContinue } Log "" Log "=== POST-CLEANUP SIZES ===" Log "" $wuSizeAfter = Get-FolderSize "$env:SystemRoot\SoftwareDistribution" $cbsSizeAfter = Get-FolderSize "$env:SystemRoot\Logs\CBS" Log "SoftwareDistribution: $wuSizeAfter MB" Log "CBS Logs: $cbsSizeAfter MB" Log "" Log "========================================" LogColor "TOTAL SPACE FREED: $totalFreed MB" "Cyan" Log "========================================" Log "" Log "Note: DISM cleanup may free additional space not counted above." Log "A restart may be required for full cleanup." explorer.exe "/select,$logFile" Read-Host "Press Enter to exit"