FAQ
Q: I've seen other simpler oneliners that look handy and I have seen others suggest different orders of execution of commands.
A: Don't use them. Most are crap
Q: Why is there so much crapy advice about this?
A: Often people operate without fully understanding what they're doing. Occasionally, they run a few commands that happen to work for their specific case or just by chance. When that happens, they often believe they've uncovered some ultimate truth and start promoting those commands as a universal solution for every problem. These people are most vocal than the experts (who have better work to do than answer questions on the Internet).
More info from
https://www.tenforums.com/performance-maintenance/161345-what-correct-order-dism-sfc-commands-fix-problems-post2687974.html#post2687974:
Because most "experts" don't understand the Windows Servicing model, and repeat tribal knowledge. [...] In the Windows OS, there is a Component Store or master repository of files. It allows Windows to patch features (files) by saving different versions of files into Side-by-Side folders. One folder for each parallel version. This allows Windows to revert updates, since the older version (which hasn't been removed) serves as a backup. You roll back to the old version by updating the registry and remembering which is the live version. An upgrade is simply extracting a new set of folders (without removing the old ones), and updating the registry to point to the new versions. Using this model, when Windows is first installed the original system files are extracted to the \Windows\WinSxS folder in many subfolders corresponding to each tiny part of Windows. Those files are then hardlinked to their final destinations under the \Windows and \Program Files folder. If we presume WinSxS is undamaged then sfc /scannow will compare WinSxS against the live system and attempt to replace incorrect or missing files as needed. "Incorrect" is simply the existing file, for whatever reason, doesn't match the archived WinSxS copy. Maybe you manually replaced a file, and that counts as incorrect. Sometimes WinSxS itself was corrupted, because a bad Windows Update or software install has placed bad data into the repository. [...] You may have to run a DISM /RestoreHealth from an online or offline source [to verify] your WinSxS is correct. Regarding DISM: /ScanHealth only reports the results of a self check. [...] /StartComponentCleanup [...] is "garbage collection" to recover disk space. /RestoreHealth is the only one that [verifies your WinSxS is correct]. The ideal sequence would be DISM /RestoreHealth to make sure the baseline WinSxS is correct, then [sfc /scannow to use the healthy WinSxS].
Q: Is there any official guide from Microsoft
A: Yes:
https://support.microsoft.com/en-us/topic/use-the-system-file-checker-tool-to-repair-missing-or-corrupted-system-files-79aa86cb-ca52-166a-92a3-966e85d4094e
Q: What is the TLDR meaning of the process you suggest here?
A: Here it is:
- You run CHKDSK to make sure the filesystem (NTFS) is healthy. (It usually is but if it's not, some of the thousands of file operations that DISM and SFC perform, may fail).
- You run DISM /Restorehealth to make sure the backup of system files that windows keeps (AKA component store, AKA WinSxS) is healthy. (SFC needs a healthy backup to correct problems).
- Finally you run SFC /scannow to check and repair Windows files by copying from these healthy backups.
Q: Microsoft doesn't suggest we run chkdsk -- why do you?
A: I guess Microsoft considers the chances of a corrupted NTFS filesystem very small. Since chkdsk /scan is pretty fast, I opted for better safe than sorry.
Q: Your code is very condensed and I can't understand it
# Create a temp log file
$log = New-TemporaryFile
# Run chkdsk /scan and capture output
chkdsk C: /scan 2>&1 | Tee-Object -FilePath $log
# Examine the ouput of chkdsk
if (Select-String 'and found no problems' $log) {
# No disk problems, proceed with DISM -- again capture output
DISM /Online /Cleanup-Image /RestoreHealth | Tee-Object -FilePath $log
# Examine the ouput of DISM
if (Select-String 'The operation completed successfully' $log) {
# All OK from DISM, run SFC
sfc /scannow
}
else {
Write-Host -ForegroundColor Red "DISM failed"
}
}
elseif (Select-String 'Windows found problems' $log) {
# CHKDSK found problems, schedule a fix on next reboot
echo Y | chkdsk C: /f
Write-Host "Reboot and start over."
}
else {
Write-Host "Cannot understand CHKDSK output:`n" (Get-Content $log)
}