How to Clean Up Bloated Windows Installer MSI Cache (C:\Windows\Installer) and WinSxS Folders Programmatically


2 views

Every Windows administrator eventually discovers three notorious folders consuming excessive disk space:

  • C:\Windows\Installer - MSI package cache
  • C:\Windows\WinSxS - Component store
  • C:\Windows\System32\DriverStore - Driver repository

The MSI cache contains:

- Original installation packages (*.msi)
- Installation transforms (*.mst) 
- Patching files (*.msp)
- Temporary installation files

Manual method using DISM:

DISM.exe /Online /Cleanup-Image /AnalyzeComponentStore
DISM.exe /Online /Cleanup-Image /StartComponentCleanup
DISM.exe /Online /Cleanup-Image /StartComponentCleanup /ResetBase

PowerShell script for automated cleanup:

# Clean WinSxS component store
$WinSxSCleanup = Start-Job -ScriptBlock {
    dism.exe /online /cleanup-image /startcomponentcleanup
}

# Clear Windows Installer cache
$InstallerFiles = Get-ChildItem "C:\Windows\Installer" -Recurse
foreach ($file in $InstallerFiles) {
    try {
        $file.Delete()
    } catch {
        Write-Warning "Could not delete $($file.FullName)"
    }
}

To remove old driver versions:

pnputil.exe /enum-drivers
pnputil.exe /delete-driver oem0.inf /uninstall /force

Using PowerShell to identify largest files:

Get-ChildItem C:\Windows\Installer -Recurse | 
Sort-Object Length -Descending | 
Select-Object -First 20 | 
Format-Table FullName, @{Name="SizeGB";Expression={[math]::Round($_.Length/1GB,2)}}

Scheduled task for regular maintenance:

$Trigger = New-ScheduledTaskTrigger -Daily -At 2am
$Action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Scripts\CleanWinCache.ps1"
Register-ScheduledTask -TaskName "Windows Cache Cleanup" -Trigger $Trigger -Action $Action -RunLevel Highest
  • Always create a system restore point before cleanup
  • Some MSI packages are required for application updates/repairs
  • Certain WinSxS components are critical for Windows Update
  • Consider using disk cleanup utility first: cleanmgr /sageset:1

Every Windows administrator eventually encounters the growing disk space consumption from three critical system folders:

  • C:\Windows\Installer (MSI package cache)
  • C:\Windows\WinSxS (Windows component store)
  • C:\Windows\System32\DriverStore (Driver repository)

The Installer folder contains cached .msi and .msp files used for application patching, modification, and uninstallation. While essential for system maintenance, it can grow uncontrollably.

Option 1: Using DISM (Recommended for WinSxS)

dism /online /cleanup-image /analyzecomponentstore
dism /online /cleanup-image /startcomponentcleanup
dism /online /cleanup-image /startcomponentcleanup /resetbase

Option 2: PowerShell Script for MSI Cache

# List all cached MSI packages
$msiFiles = Get-ChildItem -Path "$env:SystemRoot\Installer" -Filter "*.msi"
$msiFiles | Select-Object Name, Length | Sort-Object Length -Descending | Format-Table -AutoSize

# WARNING: Manual deletion not recommended - may break patch/uninstall functionality
# Consider using PatchCleaner tool instead (https://www.homedev.com.au/free/patchcleaner)

For outdated driver packages:

# List all third-party drivers
pnputil /enum-drivers

# Remove specific driver package
pnputil /delete-driver oemNN.inf /uninstall /force
  • PatchCleaner: Identifies orphaned Windows Installer patches
  • Windows Disk Cleanup Utility: Built-in tool (cleanmgr.exe) with "Windows Update Cleanup" option
  • WSUS Offline Update: For managing update caches in domain environments

Before cleaning any system folders:

  • Create a system restore point
  • Back up critical data
  • Schedule maintenance during off-hours
  • Test changes on non-production systems first

For enterprise management, consider deploying this PowerShell script via GPO:

# Scheduled cleanup script for domain workstations
$ErrorActionPreference = "SilentlyContinue"
$LastRunDate = Get-Date -Format "yyyyMMdd"
$LogPath = "\\fileserver\logs\workstation_cleanup\$env:COMPUTERNAME.log"

# Perform component store cleanup
dism /online /cleanup-image /startcomponentcleanup | Out-File $LogPath -Append

# Clean up Windows Update cache
Stop-Service -Name wuauserv -Force
Remove-Item "$env:SystemRoot\SoftwareDistribution\Download\*" -Recurse -Force
Start-Service -Name wuauserv

# Log results
"Cleanup completed on $LastRunDate" | Out-File $LogPath -Append