Thursday, May 5, 2016

WSUS Clean - Powershell script

 This little script will perform a clean up of declined and superceded updates in the WSUS database.  It uses the .Net class "Microsoft.UpdateServices.Administration" assembly, which should be loaded on your WSUS server.  Note that this script will stop the WSUS service twice, but these services will be restarted correctly by the script, and no user action is needed.

$outFilePath = '.\wsusClean.txt' 
[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration"| out-null 
$wsus = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer(); 
$cleanupScope = new-object Microsoft.UpdateServices.Administration.CleanupScope; 
$cleanupScope.DeclineSupersededUpdates = $true        
$cleanupScope.DeclineExpiredUpdates         = $true 
$cleanupScope.CleanupObsoleteUpdates     = $true 
$cleanupScope.CompressUpdates                  = $true 
#$cleanupScope.CleanupObsoleteComputers = $true 
$cleanupScope.CleanupUnneededContentFiles = $true 
$cleanupManager = $wsus.GetCleanupManager(); 
$cleanupManager.PerformCleanup($cleanupScope| Out-File -FilePath $outFilePath