WSUS Server Cleanup: Running It via PowerShell

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 […]

Wsus Server Clean Up Using Powershell

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 Amazon

As an Amazon Associate, TechyGeeksHome earns from qualifying purchases.


Discover more from TechyGeeksHome

Subscribe to get the latest posts sent to your email.

Andrew Armstrong

Andrew Armstrong is a UK-based IT professional with 26+ years of hands-on experience in Windows, Windows Server, SCCM/ConfigMgr, Active Directory, PowerShell, and enterprise infrastructure.

He founded TechyGeeksHome in 2010 and has published over 1,500 practical guides covering real-world IT problems and solutions. When not solving IT problems,

Andrew develops free Windows utilities including Ultimate Settings Panel, which has been downloaded over 850,000 times.

2 thoughts on “WSUS Server Cleanup: Running It via PowerShell

  1. 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.

  2. 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.

Comments are closed.