Automatically run System File Checker (SFC) and DISM to repair corrupt Windows system files.

Overview

This script runs SFC /scannow and DISM to detect and repair corrupt Windows system files. Essential for resolving many Windows errors.

Use Case: When experiencing random errors, crashes, or system instability.

Platform: Windows 7/8/10/11, Windows Server 2012 R2+ Requirements: Administrator privileges Execution Time: 15-45 minutes (depending on system)

The Script

Lang: powershell
  1<#
  2.SYNOPSIS
  3    Run System File Checker and DISM
  4
  5.DESCRIPTION
  6    Runs SFC /scannow and DISM to detect and repair corrupt system files.
  7
  8.PARAMETER SkipDISM
  9    If specified, only runs SFC without DISM
 10
 11.EXAMPLE
 12    .\Repair-SystemFiles.ps1
 13
 14.EXAMPLE
 15    .\Repair-SystemFiles.ps1 -SkipDISM
 16
 17.NOTES
 18    Author: glyph.sh
 19    Requires: Administrator privileges
 20    Reference: https://glyph.sh/kb/windows-common-issues/
 21#>
 22
 23[CmdletBinding()]
 24param(
 25    [switch]$SkipDISM
 26)
 27
 28#Requires -RunAsAdministrator
 29
 30Write-Host "========================================" -ForegroundColor Cyan
 31Write-Host " System File Repair Script" -ForegroundColor Cyan
 32Write-Host "========================================" -ForegroundColor Cyan
 33Write-Host ""
 34Write-Host "This process may take 15-45 minutes" -ForegroundColor Yellow
 35Write-Host ""
 36
 37if (-not $SkipDISM) {
 38    # Run DISM CheckHealth
 39    Write-Host "[1/4] Running DISM CheckHealth..." -ForegroundColor Yellow
 40    Write-Host "  This will take a few minutes..." -ForegroundColor Gray
 41    try {
 42        $dismCheck = DISM /Online /Cleanup-Image /CheckHealth
 43        if ($LASTEXITCODE -eq 0) {
 44            Write-Host "  DISM CheckHealth completed" -ForegroundColor Green
 45        } else {
 46            Write-Host "  DISM CheckHealth found issues" -ForegroundColor Yellow
 47        }
 48    }
 49    catch {
 50        Write-Host "  DISM CheckHealth failed" -ForegroundColor Red
 51    }
 52
 53    Write-Host ""
 54
 55    # Run DISM ScanHealth
 56    Write-Host "[2/4] Running DISM ScanHealth..." -ForegroundColor Yellow
 57    Write-Host "  This will take 5-10 minutes..." -ForegroundColor Gray
 58    try {
 59        $dismScan = DISM /Online /Cleanup-Image /ScanHealth
 60        if ($LASTEXITCODE -eq 0) {
 61            Write-Host "  DISM ScanHealth completed" -ForegroundColor Green
 62        } else {
 63            Write-Host "  DISM ScanHealth found issues" -ForegroundColor Yellow
 64        }
 65    }
 66    catch {
 67        Write-Host "  DISM ScanHealth failed" -ForegroundColor Red
 68    }
 69
 70    Write-Host ""
 71
 72    # Run DISM RestoreHealth
 73    Write-Host "[3/4] Running DISM RestoreHealth..." -ForegroundColor Yellow
 74    Write-Host "  This will take 10-20 minutes..." -ForegroundColor Gray
 75    try {
 76        $dismRestore = DISM /Online /Cleanup-Image /RestoreHealth
 77        if ($LASTEXITCODE -eq 0) {
 78            Write-Host "  DISM RestoreHealth completed" -ForegroundColor Green
 79        } else {
 80            Write-Host "  DISM RestoreHealth completed with warnings" -ForegroundColor Yellow
 81        }
 82    }
 83    catch {
 84        Write-Host "  DISM RestoreHealth failed" -ForegroundColor Red
 85    }
 86
 87    Write-Host ""
 88}
 89
 90# Run SFC
 91$sfcStep = if ($SkipDISM) { "1/1" } else { "4/4" }
 92Write-Host "[$sfcStep] Running System File Checker..." -ForegroundColor Yellow
 93Write-Host "  This will take 10-20 minutes..." -ForegroundColor Gray
 94
 95try {
 96    $sfcOutput = sfc /scannow
 97    $sfcLog = Get-Content "$env:SystemRoot\Logs\CBS\CBS.log" -Tail 50 -ErrorAction SilentlyContinue
 98
 99    if ($sfcOutput -match "did not find any integrity violations") {
100        Write-Host "  No integrity violations found" -ForegroundColor Green
101    }
102    elseif ($sfcOutput -match "found corrupt files and successfully repaired them") {
103        Write-Host "  Found and repaired corrupt files" -ForegroundColor Green
104    }
105    elseif ($sfcOutput -match "found corrupt files but was unable to fix some of them") {
106        Write-Host "  Found corrupt files but could not repair all" -ForegroundColor Yellow
107        Write-Host "  Check CBS.log for details" -ForegroundColor Yellow
108    }
109    else {
110        Write-Host "  SFC scan completed" -ForegroundColor Green
111    }
112}
113catch {
114    Write-Host "  SFC scan failed" -ForegroundColor Red
115}
116
117Write-Host ""
118Write-Host "========================================" -ForegroundColor Cyan
119Write-Host " System File Repair Complete" -ForegroundColor Cyan
120Write-Host "========================================" -ForegroundColor Cyan
121Write-Host ""
122Write-Host "Logs are located at:" -ForegroundColor Yellow
123Write-Host "  SFC: $env:SystemRoot\Logs\CBS\CBS.log" -ForegroundColor White
124Write-Host "  DISM: $env:SystemRoot\Logs\DISM\dism.log" -ForegroundColor White
125Write-Host ""
126Write-Host "If issues persist:" -ForegroundColor Yellow
127Write-Host "1. Review the log files for details" -ForegroundColor White
128Write-Host "2. Run Windows Update" -ForegroundColor White
129Write-Host "3. Consider in-place upgrade repair" -ForegroundColor White
130Write-Host ""

Usage

Full Repair (DISM + SFC)

Lang: powershell
1.\Repair-SystemFiles.ps1

SFC Only

Lang: powershell
1.\Repair-SystemFiles.ps1 -SkipDISM

What It Does

  1. DISM CheckHealth: Quick check for corruption
  2. DISM ScanHealth: Detailed scan for corruption
  3. DISM RestoreHealth: Repairs component store
  4. SFC Scan: Scans and repairs system files

When to Use Each Tool

DISM First: Run DISM before SFC to repair the component store that SFC uses.

SFC Second: Run SFC after DISM to repair actual system files.

If SFC Fails: Run DISM again, then retry SFC.

Common Error Codes

ErrorMeaningSolution
0x80070005Access deniedRun as administrator
0x800F081FSource files not foundRun Windows Update first
0x800F0906DISM failedBoot to Safe Mode and retry

After Running

  1. Reboot: Recommended after file repairs
  2. Windows Update: Run updates to get latest files
  3. Test: Verify the original issue is resolved

See Also

Download

Lang: powershell
1Invoke-WebRequest -Uri "https://glyph.sh/scripts/Repair-SystemFiles.ps1" -OutFile "Repair-SystemFiles.ps1"
2.\Repair-SystemFiles.ps1