Windows Server Update Services (WSUS) is a critical component for managing updates, but over time, its database and storage can become bloated with superseded updates, expired updates, and obsolete computer objects. If left unmaintained, WSUS can suffer from severe performance degradation and excessive disk space usage.
Why Automate WSUS Maintenance?
Manual maintenance via the WSUS Server Cleanup Wizard often times out or fails on large, unmaintained servers. By leveraging PowerShell, system administrators can automate the cleanup process, run it during off-hours, and ensure the WSUS database remains optimized.
How to Create the WSUS Cleanup Script
PowerShell provides a built-in cmdlet called Invoke-WsusServerCleanup which performs the same tasks as the graphical wizard. We can wrap this in a script for automation.
- Open PowerShell as Administrator on your WSUS server.
- Create a new PowerShell script file, for example:
C:\Scripts\WSUS-Cleanup.ps1. - Add the following code to connect to the local WSUS server and run the cleanup tasks:
# Connect to the local WSUS Server
$wsus = Get-WsusServer
# Run all cleanup tasks
Invoke-WsusServerCleanup -UpdateServer $wsus -CleanupObsoleteComputers -CleanupObsoleteUpdates -CleanupUnneededContentFiles -CompressUpdates -DeclineExpiredUpdates -DeclineSupersededUpdates
How to Schedule the Cleanup using Task Scheduler
To ensure WSUS stays healthy, this script should run automatically, typically once a month.
- Open Task Scheduler (
taskschd.msc). - Click Create Task… in the right-hand Actions pane.
- On the General tab, name the task (e.g., “WSUS Monthly Cleanup”) and select Run whether user is logged on or not. Check the box for Run with highest privileges.
- On the Triggers tab, click New and set it to run Monthly during a maintenance window.
- On the Actions tab, click New.
- Action: Start a program
- Program/script:
powershell.exe - Add arguments:
-ExecutionPolicy Bypass -WindowStyle Hidden -File "C:\Scripts\WSUS-Cleanup.ps1"
- Click OK and enter your Administrator credentials to save the task.
Your WSUS server will now automatically purge old updates and computers, keeping your update infrastructure lean and responsive.