I took some steps to automate my WSUS Server Cleanup Wizard tasks. It can take forever if you don’t stay on top of it and can crash frequently if you don’t do it often enough. With the following steps, I was able to automate the process and wanted to share my results!
First was create a small PowerShell script. Using this code, it will clean everything that the normal Gui process cleans. Be sure to add the extra Return character at the end so it knows to run the last line. Otherwise, it just loads the command and sits there never finishing. Save it as a .ps1 to the local C:\ drive, or wherever you would like to save the file at. You can create the script fairly easily. Open up a new Notepad window and paste the text. Click File>Save As and change the “Save as type” to “All Files”. Put the name of WSUS-Cleanup.ps1 and that is it!
Invoke-WsusServerCleanup -CleanupObsoleteComputers
Invoke-WsusServerCleanup -CleanupObsoleteUpdates
Invoke-WsusServerCleanup -CleanupUnneededContentFiles
Invoke-WsusServerCleanup -CompressUpdates
Invoke-WsusServerCleanup -DeclineExpiredUpdates
Invoke-WsusServerCleanup -DeclineSupersededUpdates
Next is to add a scheduled task to automate the process fully. Simply create a scheduled task using the Task Scheduler in Windows. Set the trigger to however often you would like it to run. I run once daily since that seems to keep WSUS in check for the most part. In actions set it to Start a program. Set the “Program/script:” box to “PowerShell”. In Add arguments, add “-File ” so something like “-File C:\WSUS-Cleanup.ps1”.

And that is it! After you have created/saved the script and created the scheduled task you are good to go! You can manually run the created script to ensure functionality (does require admin privileges). I have so far had successful results with this process and keeping things clean without me having to manually keep tabs on it. If the cleanup wizard hasn’t been run at all or in a long time the script may time out. I recommend running the script a few times before forgetting about it. WSUS likes to timeout on things, but the more you run the cleanup, the more it will get cleaned and eventually it will work without errors. I speak from experience on that part. Feel free to run and share!
