Worklet - Windows Update Component Cleanup

This worklet takes advantage of starting Windows Component Cleanup on command with output in the Automox Activity Log. This will clean up only if the updated components is older then 30 days.

In the example output you can see this process cleaned up 1.28 GB of space.
image

Optional change to clean everything – Add /ResetBase after the /StartComponentCleanup switch. The /ResetBase switch option also deletes all superseded versions of every component in the component store. A warning if you want to use it on systems in production :no_entry: All existing service packs and updates cannot be uninstalled after this command is completed .

Remediation Code

function Get-ComponentStore{
  # Analyze the size of the component store (WinSxS folder)
  $output = & Dism.exe /Online /Cleanup-Image /AnalyzeComponentStore
  
  # Regex to capture anything after " : "
  [regex]$regex = "(?m)(?<=\b : ).*$"
  
  # Build Object
  [pscustomobject] @{
    'Reported Size' = ($regex.match(($output | Where-Object {$_ -match "Windows Explorer Reported Size of Component Store"}))).Value
    'Actual Size' = ($regex.match(($output | Where-Object {$_ -match "Actual Size of Component Store"}))).Value
    'Reclaimable Packages' = ($regex.match(($output | Where-Object {$_ -match "Number of Reclaimable Packages"}))).Value
    'Cleanup Recommended' = ($regex.match(($output | Where-Object {$_ -match "Component Store Cleanup Recommended"}))).Value
  }
}

function Start-ComponentCleanup{
    # Delete update components
    $output = & Dism.exe /online /Cleanup-Image /StartComponentCleanup
}

# Get current status of Component cleanup
$results = Get-ComponentStore
Write-Output "Reported Size $($results.'Reported Size'). Actual Size $($results.'Actual Size'). Reclaimable Packages $($results.'Reclaimable Packages'). Cleanup Recommended $($results.'Cleanup Recommended'). "

IF($results.'Cleanup Recommended' -eq 'Yes'){
    Write-Output "Component cleanup Detected as required. Starting component cleanup. "
    Start-ComponentCleanup
    $results = Get-ComponentStore
    Write-Output "Reported Size $($results.'Reported Size'). Actual Size $($results.'Actual Size'). Reclaimable Packages $($results.'Reclaimable Packages'). Cleanup Recommended $($results.'Cleanup Recommended'). "
}
2 Likes