Repair-WindowsComponentStore.ps1
|
<#PSScriptInfo
.VERSION 1.0.0 .GUID 7ffca9c9-f000-46dd-aa17-ec623e3a09ee .AUTHOR waresoft .COMPANYNAME waresoft .COPYRIGHT (c) 2026 waresoft. All rights reserved. .TAGS PowerShell Windows Windows10 Windows11 SysAdmin Maintenance DISM SFC WinSxS SystemRepair Troubleshooting Automation .LICENSEURI https://opensource.org/licenses/MIT .RELEASENOTES Initial release (v1.0.0): - Auto-elevates to Administrator if launched in a standard session. - Runs DISM /ScanHealth (using /English for locale-safe parsing) and conditionally triggers /RestoreHealth only when corruption is detected. - Runs SFC /scannow to verify and repair protected system files. - Runs DISM /AnalyzeComponentStore and conditionally triggers /StartComponentCleanup only when cleanup is recommended. - Reports exit codes and pauses on completion so results remain visible. #> # Requires Administrator Privilege - Auto-elevate if not running as Admin <# .DESCRIPTION Automates Windows Component Store (WinSxS) diagnostics, repair, and cleanup in a single guided run. Automatically elevates to Administrator, runs DISM /ScanHealth (triggering /RestoreHealth only when corruption is found), verifies protected system files with SFC /scannow, and executes DISM /StartComponentCleanup only if /AnalyzeComponentStore recommends it. #> $IsAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $IsAdmin) { Write-Host "[!] ERROR: This script must be run as Administrator." -ForegroundColor Red Write-Host "Attempting to restart script with elevated privileges..." -ForegroundColor Yellow Start-Sleep -Seconds 2 Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs exit } # Set console title and background color scheme $Host.UI.RawUI.WindowTitle = "Windows Component Store Maintenance & Repair" [Console]::ForegroundColor = [ConsoleColor]::Green function Pause-Script ($Message = "Press any key to continue...") { Write-Host "" Write-Host $Message -ForegroundColor Cyan $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") } Write-Host "=======================================================================" Write-Host " WINDOWS COMPONENT STORE MAINTENANCE & REPAIR" Write-Host "=======================================================================" Write-Host "" Write-Host "Execution Sequence:" Write-Host " 1. DISM /ScanHealth - Checks if component store needs repair." Write-Host " -> DISM /RestoreHealth - Runs ONLY if corruption is detected." Write-Host " 2. SFC /ScanNow - Repairs system files using healthy store." Write-Host " 3. DISM /AnalyzeComponentStore - Checks if cleanup is required." Write-Host " -> /StartComponentCleanup - Runs ONLY if cleanup is recommended." Write-Host "" Write-Host "=======================================================================" Write-Host "Starting automated maintenance..." -ForegroundColor Cyan Start-Sleep -Seconds 3 # Step 1: DISM ScanHealth / RestoreHealth Write-Host "" Write-Host "[1/3] Step 1: Scanning Component Store Health (DISM)..." -ForegroundColor Yellow Write-Host "-----------------------------------------------------------------------" Write-Host "Scanning... (The progress bar is hidden while the script reads the results. Please wait...)`n" -ForegroundColor DarkGray # Capture output as an array and convert to a single string block $healthCheck = dism.exe /English /Online /Cleanup-Image /ScanHealth $healthCheckText = $healthCheck -join "`n" Write-Host $healthCheckText -ForegroundColor Gray if ($healthCheckText -match "No component store corruption detected") { Write-Host "`n[+] Component store is healthy. Skipping RestoreHealth." -ForegroundColor Green } else { Write-Host "`n[!] Corruption or issues detected. Starting RestoreHealth..." -ForegroundColor Yellow dism.exe /Online /Cleanup-Image /RestoreHealth if ($LASTEXITCODE -ne 0) { Write-Host "`n[!] Warning: DISM RestoreHealth encountered an error (Code: $LASTEXITCODE)." -ForegroundColor Red Write-Host "You may need to specify a source (e.g., Windows ISO) or check your internet connection." -ForegroundColor Yellow } else { Write-Host "`n[+] Component Store repair command completed successfully." -ForegroundColor Green Write-Host " Note: If it still reports as 'repairable' later, restart your PC to apply the fixes." -ForegroundColor Cyan } } # Step 2: SFC ScanNow Write-Host "" Write-Host "[2/3] Step 2: Running System File Checker (SFC)..." -ForegroundColor Yellow Write-Host "-----------------------------------------------------------------------" sfc.exe /scannow if ($LASTEXITCODE -ne 0) { Write-Host "`n[!] Warning: SFC encountered an issue (Code: $LASTEXITCODE)." -ForegroundColor Red } else { Write-Host "`n[+] System File Checker completed successfully." -ForegroundColor Green } # Step 3: DISM AnalyzeComponentStore / StartComponentCleanup Write-Host "" Write-Host "[3/3] Step 3: Analyzing Component Store for Cleanup..." -ForegroundColor Yellow Write-Host "-----------------------------------------------------------------------" Write-Host "Analyzing... (The progress bar is hidden while the script reads the results. Please wait...)`n" -ForegroundColor DarkGray $cleanupCheck = dism.exe /English /Online /Cleanup-Image /AnalyzeComponentStore $cleanupCheckText = $cleanupCheck -join "`n" Write-Host $cleanupCheckText -ForegroundColor Gray if ($cleanupCheckText -match "Cleanup Recommended\s*:\s*Yes") { Write-Host "`n[!] Cleanup recommended. Starting Safe Cleanup..." -ForegroundColor Yellow Write-Host "Note: It is completely normal for DISM to suddenly exit or jump to 100% when hitting 20% or 70%.`n" -ForegroundColor Cyan dism.exe /Online /Cleanup-Image /StartComponentCleanup if ($LASTEXITCODE -ne 0) { Write-Host "`n[!] Warning: Component cleanup encountered an issue (Code: $LASTEXITCODE)." -ForegroundColor Red } else { Write-Host "`n[+] Component Store cleanup completed successfully." -ForegroundColor Green } } else { Write-Host "`n[+] Cleanup is not required at this time. Skipping cleanup." -ForegroundColor Green } # Summary Write-Host "" Write-Host "=======================================================================" Write-Host " MAINTENANCE COMPLETE" Write-Host "=======================================================================" Write-Host "Verification: Check the output above for SFC results." Write-Host "- `"Did not find any integrity violations`" = Clean system." Write-Host "- `"Successfully repaired them`" = System files patched from WinSxS." Write-Host "" Write-Host "IMPORTANT: If repairs were made, please Restart your PC." -ForegroundColor Yellow Write-Host "" Pause-Script "Script finished. Press any key to close this window..." |