WSUS – Server Clean Up Using Powershell

microsoft windows white logo
microsoft windows white logo

Estimated reading time: 2 minutes

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!

Comments

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

Share this content:

Click to rate this post!
[Total: 1 Average: 5]
Avatar for Andrew Armstrong

About Andrew Armstrong

Founder of TechyGeeksHome and Head Editor for over 15 years! IT expert in multiple areas for over 26 years. Sharing experience and knowledge whenever possible! Making IT Happen.

View all posts by Andrew Armstrong

2 Comments on “WSUS – Server Clean Up Using 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.

Leave a Reply

Your email address will not be published. Required fields are marked *