WSUS – Cleanup using Powershell with email notification output
If you are using WSUS in your environment, you may want to run a scheduled task to cleanup old updates and compress some of the larger updates to free up space on your server. A good way of doing this is to run a Powershell script which will take care of all your cleanup requirements and as we are using Powershell, we can even get it to send us an email once it is complete with the output information on what the script has managed to clean up.
First of all, you need to decide on what you actually want to do with the script. This includes whether you want to run it manually or add it as a scheduled task or even a System Center Orchestrator Runbook.
For the purposes of this guide, we are going to run the Powershell script with all available options but manually.
For a local WSUS installation, you should use the script below:
Get-WsusServer | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles -CompressUpdates -DeclineExpiredUpdates -DeclineSupersededUpdates
If you want to run the script on a remote server, you can state the server name like below:
Get-WsusServer "WSUSServer" | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles -CompressUpdates -DeclineExpiredUpdates -DeclineSupersededUpdates
As you can see from the script above, we are running the cleanup on all options available to us, i.e. Declined Updates. You can add and remove these as you so wish. The parameters available to you are:
So the above will run manually and then output something like the following within your Powershell window:
But what if you want to receive an email with this information on so that you can stay up to date with what the cleanup is doing, especially if you are running it to a schedule. Using Powershell, you can add additional script to your cleanup script to send an email.
To do this, you need to amend your script so it has the email parameters of your local email server and also build an output to the body of the email, so your script should look like this:
$output = Get-WsusServer | Invoke-WsusServerCleanup -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles -CompressUpdates -DeclineExpiredUpdates -DeclineSupersededUpdates
You should change the email parameters to suit your local email environment.
When this script is run, it will then output to an email as specified.
You can download the Powershell script as a PS1 file by clicking the link below:
If you have any questions or feedback on this guide, please feel free to leave us a message below and we will get back to you when we can.