PowerShell · Endpoints
Clear Windows Update cache
Use this when Windows Update is stuck, showing errors, or consuming excess disk space. Run as administrator. The script stops dependent services gracefully, clears only the Download folder so update history is preserved, and brings services back up. A reboot is recommended after running to let Windows re-evaluate pending updates.
Script
Code
#Requires -RunAsAdministrator
$services = @("wuauserv", "cryptSvc", "bits", "msiserver")
$cachePath = "$env:SystemRootSoftwareDistributionDownload"
Write-Output "Stopping Windows Update services..."
foreach ($svc in $services) {
Stop-Service -Name $svc -Force -ErrorAction SilentlyContinue
Write-Output " Stopped: $svc"
}
if (Test-Path $cachePath) {
$sizeBefore = (Get-ChildItem $cachePath -Recurse -ErrorAction SilentlyContinue |
Measure-Object -Property Length -Sum).Sum
Remove-Item "$cachePath*" -Recurse -Force -ErrorAction SilentlyContinue
$sizeMB = [math]::Round($sizeBefore / 1MB, 1)
Write-Output "Cleared $sizeMB MB from $cachePath"
} else {
Write-Output "Cache path not found: $cachePath"
}
Write-Output "Restarting Windows Update services..."
foreach ($svc in $services) {
Start-Service -Name $svc -ErrorAction SilentlyContinue
Write-Output " Started: $svc"
}
Write-Output "Done. A reboot is recommended."Usage
How to use this script
Use this when Windows Update is stuck, showing errors, or consuming excess disk space. Run as administrator. The script stops dependent services gracefully, clears only the Download folder so update history is preserved, and brings services back up. A reboot is recommended after running to let Windows re-evaluate pending updates.
Review the script and test in a non-production environment before running at scale.
Tags
endpointswindows-updatemaintenance
More scripts