WSUS ships with a built-in Server Cleanup Wizard in the console, but if you’d rather automate the cleanup — across a scheduled task, an Orchestrator runbook, or Service Manager — PowerShell gives you full control over the same cleanup operations the wizard performs.
The script
Save the following as a .ps1 file and run it as Administrator on your WSUS server:
$outFilePath = '.wsuscleanup.txt'
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration") | out-null
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer();
$cleanupScope = new-object Microsoft.UpdateServices.Administration.CleanupScope;
$cleanupScope.DeclineSupersededUpdates = $true
$cleanupScope.DeclineExpiredUpdates = $true
$cleanupScope.CleanupObsoleteUpdates = $true
$cleanupScope.CompressUpdates = $true
$cleanupScope.CleanupObsoleteComputers = $true
$cleanupScope.CleanupUnneededContentFiles = $true
$cleanupManager = $wsus.GetCleanupManager();
$cleanupManager.PerformCleanup($cleanupScope) | Out-File -FilePath $outFilePath
Each property on $cleanupScope maps directly to one of the checkboxes in the GUI wizard — decline superseded/expired updates, remove obsolete updates and computers, compress updates, and clean up content files that are no longer referenced.
What to expect
Depending on how much cleanup is overdue, this can take a while to run, particularly the CleanupUnneededContentFiles step on a WSUS server that hasn’t been cleaned in a long time — let it finish rather than cancelling partway through. Once complete, the script writes a summary to wsuscleanup.txt in the folder you ran it from, and you should see a noticeable drop in disk usage on the WSUS content volume.
Because it’s just a PowerShell script, you can wrap it in a scheduled task to run monthly, or drop it into an Orchestrator runbook or Service Manager workflow if you want cleanup handled as part of a wider maintenance process.
Resources
Gear We Recommend
Testing configs is easier with a dedicated admin machine set up right. Here’s the kit we use.
Browse our Windows Admin Toolkit picks on AmazonAs an Amazon Associate, TechyGeeksHome earns from qualifying purchases.
Discover more from TechyGeeksHome
Subscribe to get the latest posts sent to your email.
How does the getupdateserver deal with wsus servers in an upstreamdownstream relationship? Will it detect all the wsus servers or just on the server the powershell command is being run.
How does the getupdateserver deal with wsus servers in an upstreamdownstream relationship? Will it detect all the wsus servers or just on the server the powershell command is being run.