WSUS – Server Clean Up Using Powershell


If you use WSUS then you will be familiar with running Server Cleanup Wizard which is fine but what if you want to use Powershell? well, you can also use that to run the server cleanup on all your WSUS servers.

By using Powershell you have the option to then automate its running using basic windows task scheduler or something a bit more advanced like Orchestrator Runbooks or build it all in to Service Manager.

So, all you need to do is to take the below Powershell script, save it as a .PS1 file and then run as admin 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   

If you wish to just download the Powershell script then you can do so from here.

It may take time to complete on your server if you have a lot of cleaning up to do so give it time. The script will create a text file in the same location you ran the script from with the completed values for what it has achieved.

Once complete, check your disk space and you should notice a big change in your free space and your updates will be a lot tidier!


If you have any questions about this guide, please feel free to leave us a message below using our Disqus system.

You might also like...

1 Response

  1. K says:

    How does the getupdateserver deal with wsus servers in an upstream\downstream relationship? Will it detect all the wsus servers or just on the server the powershell command is being run.

Leave us a message...

This site uses Akismet to reduce spam. Learn how your comment data is processed.