Fix strange Windows/Office issues due to corrupted windows files


.

FAQ

Q: I've seen oneliners that look handy.

E.g. like this: sfc /scannow; DISM.exe /Online /Cleanup-image /Restorehealth; sfc /scannow; chkdsk C: /r /x /v; shutdown /r

A: Don't use them. For example the above contains a redantad call to sfc, doesn't check the disk before messing a lot with it and doesn't allow you to check for errors from DISM (it also scans for HDD bad sectors - which is not really useful on an SSD)

Q: What is the official guide from Microsoft

A: 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: I have seen others suggest different orders of execution for sfc and dism. Why?

A: 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. The old adage that applies is "Garbage In, Garbage Out." If you have bad data inside WinSxS, then running sfc doesn't help. You may have to run a DISM /RestoreHealth from an online or offline source for a second opinion on whether your WinSxS is correct.

/ScanHealth only reports the results of a self check. It doesn't invoke any repair actions, and assumes WinSxS is correct.

/StartComponentCleanup only tries to remove superseded (outdated) components if there are multiple versions of old WinSxS components, and some don't need to be saved for safekeeping. This is "garbage collection" to recover disk space.

/RestoreHealth is the only one that does real work.

The ideal sequence would be DISM /RestoreHealth to make sure the baseline WinSxS is correct, then [sfc /scannow to use the correct baseline].

https://www.tenforums.com/performance-maintenance/161345-what-correct-order-dism-sfc-commands-fix-problems-post2687974.html#post2687974

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. Most likely Windows Update is damaged."
    }
}
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)
}

Topic revision: r20 - 02 Sep 2025, NickDemou
Copyright © enLogic