Windows: Cleanup files from WSUS which are no longer needed

Hi,

the content of an WSUS Server crows every month for several GiBytes because WSUS does held all files which has the approval state “install”.

The GUI way to cleanup the WSUS Content folder is to first decline all expired and superseded updates and than run the WSUS Cleanup Wizard.

But you can also use the script way 🙂

[reflection.assembly]::LoadWithPartialName("Microsoft.UpdateServices.Administration")

# Only needed if the script doesn't run on the WSUS Server
[String]$sWSUSServer = "yourWSUSServer"
[Boolean]$bSecureConnection = $False
[Int32]$iWSUSPort = 8531
try
{
       if([String]::IsNUllOrEmpty($sWSUSServer))
       {
            $oWSUSServer = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer()
       }
       else
       {
	    $oWSUSServer = [Microsoft.UpdateServices.Administration.AdminProxy]::GetUpdateServer($sWSUSServer,$bSecureConnection,$iWSUSPort)
       }
}
catch [Exception]
{
	write-warning ("Cannot connect to WSUS Server: $sWSUSServer. Error: " + $_)
        exit 1
}


$oCleanUpManger=$oWSUSServer.GetCleanUpManager()

$oCleanUpScope=new-object Microsoft.UpdateServices.Administration.CleanupScope
$oCleanUpScope.DeclineSupersededUpdates=$true
$oCleanUpScope.DeclineExpiredUpdates=$true
$oCleanUpScope.CleanupUnneededContentFiles=$true
$oCleanUpScope.CleanupLocalPublishedContentFiles=$true
$oCleanUpScope.CleanupObsoleteUpdates=$false
$oCleanUpScope.CompressUpdates=$false
$oCleanUpScope.CleanupObsoleteComputers=$false

$oCleanUpManger.PerformCleanup($oCleanUpScope)

Run the script

PS C:\> .\WSUSCleanup.ps1
SupersededUpdatesDeclined : 18
ExpiredUpdatesDeclined    : 0
ObsoleteUpdatesDeleted    : 0
UpdatesCompressed         : 0
ObsoleteComputersDeleted  : 0
DiskSpaceFreed            : 274123310463

Michael

WSUS Cleanup
WSUS Cleanup
WSUSCleanup.ps1
Version: 1.0.0.0
1.5 KiB
27 Downloads
Details...

Time limit is exhausted. Please reload CAPTCHA.

Original Theme by Schiy · Powered by WordPress