WSUS – Server Clean Up Using Powershell

microsoft windows white logo

Last updated on April 1st, 2023 at 07:49 pm

Read Time:1 Minute, 21 Second

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.

Click to rate this post!
[Total: 1 Average: 5]

Free Subscription

If you want to be notified when we post more quality guides like this one, sign up to our free subscription service and you will receive an email when a new post is live.

Join 441 other subscribers.

No need to worry, we will not be filling your inbox with spam and you can unsubscribe anytime you like.


3 thoughts 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 upstream\downstream relationship? Will it detect all the wsus servers or just on the server the powershell command is being run.

  3. 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 us a message...

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