Automatic WSUS Server Cleanup

After having migrated a WSUS role to a new Windows 2012R2 Server I was looking for a way to run the WSUS Server cleanup automatically on a schedule. Once a month seems to be sufficient as Microsoft has move to one official patch day per month apart from emergencies. As part of Windows Server 2012R2 Microsoft has released some PowerShell cmdlets do do exactly this.

Based on that TechNet article I have put together the following PowerShell script.

[codesyntax lang=”powershell”]

Get-WsusServer -Name ServerName -PortNumber 8530 | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CompressUpdates -CleanupUnneededContentFile -DeclineExpiredUpdates -DeclineSupersededUpdates

[/codesyntax]

The above command line can be copied into a file and invoked either manually or via the task scheduler. Below is a sample output.

WSUS Cleanup Script

When setting up a scheduled job you may want to redirect the output to a file. To do this simple add “| Out-File C:\Scripts\clean_wsus.log” at the end. The filename and directory can of course be changed. Here is the full script.

 

[codesyntax lang=”powershell” container=”DIV”]

Get-WsusServer -Name glw-ccm-01 -PortNumber 8530 | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CompressUpdates -CleanupUnneededContentFile -DeclineExpiredUpdates -DeclineSupersededUpdates | Out-File C:\Scripts\clean_wsus.log

[/codesyntax]

 

 

  • Share:
  1. Great tip! Thank you. I was looking for some way to automate this and somehow google never pointed me to this cmdlet.

  2. Thanks for your howto! Nice to know, that Microsoft finally prepared WSUS cmdlet for PowerShell.

Leave a Comment

Send a Message