WSUS – Cleanup using Powershell with email notification output

powershell
powershell

Estimated reading time: 3 minutes

lntroduction

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.

Guide

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

Parameters

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:

  • -CleanupObsoleteComputers
  • -CleanupObsoleteUpdates
  • -CleanupUnneededContentFiles
  • -CompressUpdates
  • -DeclineExpiredUpdates
  • -DeclineSupersededUpdates

Email Parameters

So the above will run manually and then output something like the following within your Powershell window:

WSUS - Cleanup using Powershell with email notification output 1

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
Send-MailMessage -from "WSUS PS Cleanup <wsus@techygeekshome.info>" -to "blog@techygeekshome.info" -Subject "WSUS Cleanup Stats" -Body ($output | Out-String) -SmtpServer smtp.techygeekshome.info

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.

Download

You can download the Powershell script as a PS1 file by clicking the link below:

Download WSUS Cleanup PS1

Comments

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.

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

Share this content:

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

Leave a Reply

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