[Script] Windows 10 Repair Script

Discussion in 'Windows 10' started by murphy78, Aug 14, 2015.

  1. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    #1 murphy78, Aug 14, 2015
    Last edited by a moderator: Apr 20, 2017
    So, I wrote this script because:
    1 - Many of my releases use esd or split-wim format
    2 - Many Win10 installs often have a problem with a file or files not matching their catalogued hashes

    To check if your system has a mismatched hash, run admin cmd prompt (winkey+x, then a):
    Code:
    SFC /SCANNOW
    System_File_Repair.cmd:
    Code:
    @echo off
    color 1f
    SetLocal EnableDelayedExpansion
    
    
    :Begin UAC check and Auto-Elevate Permissions
    :-------------------------------------
    REM  --> Check for permissions
    >nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
    
    
    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
    echo:
    echo   Requesting Administrative Privileges...
    echo   Press YES in UAC Prompt to Continue
    echo:
    
    
        goto UACPrompt
    ) else ( goto gotAdmin )
    
    
    :UACPrompt
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        echo UAC.ShellExecute "%~s0", "", "", "runas", 1 >> "%temp%\getadmin.vbs"
    
    
        "%temp%\getadmin.vbs"
        exit /B
    
    
    :gotAdmin
        if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
        pushd "%CD%"
        CD /D "%~dp0"
    :--------------------------------------
    Title Windows System File Restore -murphy78
    :REPAIRIT
    ECHO -------------------------------------------------------------------------------
    echo.                     Windows System File Restore
    ECHO -------------------------------------------------------------------------------
    
    
    ECHO This script will repair system files by using install source files.
    ECHO If no install source files are found, it will try to use online mode,
    ECHO but it should be noted that online mode rarely works.
    ECHO -------------------------------------------------------------------------------
    CHOICE /c YN /n /M "Run this script? (Y/N): "
    IF %ERRORLEVEL% NEQ 1 EXIT
    SET INSTALLIMAGE=""
    SET SPLIT=0
    SET ESDFILE=0
    
    
    FOR %%i IN (Z Y X W V U T S R Q P O N M L K J I H G F E D C B A) DO (IF EXIST "%%i:\Sources\install.wim" SET INSTALLIMAGE="%%i:\Sources\install.wim"&GOTO :FOUNDIMG)
    FOR %%i IN (Z Y X W V U T S R Q P O N M L K J I H G F E D C B A) DO (IF EXIST "%%i:\Sources\install.esd" SET INSTALLIMAGE="%%i:\Sources\install.esd"&SET ESDFILE=1&GOTO :FOUNDIMG)
    FOR %%i IN (Z Y X W V U T S R Q P O N M L K J I H G F E D C B A) DO (IF EXIST "%%i:\Sources\install.swm" SET INSTALLIMAGE="%%i:\Sources\install.swm"&SET SPLIT=1&SET SPLITPATTERN="%%i:\Sources\install*.swm"&GOTO :FOUNDIMG)
    
    
    :IF none of the install.wim, esd, or swm, files were found in a sources\ directory, just do normal online restore
    IF NOT EXIST !INSTALLIMAGE! (
    CLS
    ECHO ===============================================================================
    echo.               No install image found: Attempting online restore
    ECHO -------------------------------------------------------------------------------
    dism /online /cleanup-image /restorehealth
    GOTO :FINISHED
    )
    
    
    :FOUNDIMG
    CLS
    ECHO Scanning !INSTALLIMAGE! to create the index list file...
    IF EXIST "%TEMP%\IMAGELIST.TXT" del /q/f "%TEMP%\IMAGELIST.TXT"
    IF EXIST "%TEMP%\install.wim" del /q/f "%TEMP%\install.wim"
    for /f "tokens=2 delims=: " %%a in ('dism /Get-WimInfo /WimFile:!INSTALLIMAGE! ^| find /i "Index"') do (
    for /f "tokens=2 delims=:" %%g in ('dism /Get-WimInfo /WimFile:!INSTALLIMAGE! /Index:%%a ^| find /i "Name"') do (ECHO %%a.%%g>>%TEMP%\IMAGELIST.TXT))
    ECHO ===============================================================================
    ECHO =============================== INDEX SELECTION ===============================
    
    
    :Display-Indexes
    TYPE %TEMP%\IMAGELIST.TXT
    ECHO ========================== SELECT INDEX OR Q TO EXIT ==========================
    SET INPUT=
    SET /P INPUT="Please select an Index Number or Q to exit: "
    IF NOT DEFINED INPUT GOTO :IMAGESELECTION
    IF /I '!INPUT!'=='Q' GOTO :FINISHED
    SET INDEX=!INPUT!
    
    
    :CONFIRMINDEX
    echo.
    Echo You have selected Index: %INDEX%
    dism /get-wiminfo /wimfile:%INSTALLIMAGE% /index:%INDEX%>NUL 2>NUL
    IF %ERRORLEVEL% NEQ 0 GOTO :FOUNDIMG
    choice /c yn /n /m "Are you sure you wish to use this index? (Y/N): "
    IF !ERRORLEVEL! NEQ 1 GOTO :FOUNDIMG
    IF %SPLIT%==1 GOTO :FOUNDSPLIT
    IF %ESDFILE%==1 GOTO :FOUNDESD
    
    
    :Using normal wim
    ECHO -------------------------------------------------------------------------------
    echo.                         Repairing System Files
    ECHO -------------------------------------------------------------------------------
    
    
    dism /online /cleanup-image /restorehealth:wim:"%INSTALLIMAGE%":1
    GOTO :FINISHED
    
    
    
    
    :FOUNDSPLIT
    ECHO -------------------------------------------------------------------------------
    echo.              Script is exporting split-wim file to a normal wim
    echo.                         This will take a while
    ECHO -------------------------------------------------------------------------------
    dism /export-image /sourceimagefile:%INSTALLIMAGE% /swmfile:%SPLITPATTERN% /sourceindex:%INDEX% /destinationimagefile:%TEMP%\install.wim /compress:max
    IF %ERRORLEVEL% NEQ 0 (ECHO There was a problem exporting this index to temp folder&pause&GOTO :FINISHED)
    ECHO -------------------------------------------------------------------------------
    echo.                         Repairing System Files
    ECHO -------------------------------------------------------------------------------
    dism /online /cleanup-image /restorehealth:wim:"%TEMP%\install.wim":1
    GOTO :FINISHED
    
    
    :FOUNDESD
    ECHO -------------------------------------------------------------------------------
    echo.                Script is exporting esd file to a normal wim
    echo.                         This will take a while
    ECHO -------------------------------------------------------------------------------
    dism /export-image /sourceimagefile:%INSTALLIMAGE% /sourceindex:%INDEX% /destinationimagefile:%TEMP%\install.wim /compress:max
    IF %ERRORLEVEL% NEQ 0 (ECHO There was a problem exporting this index to temp folder&pause&GOTO :FINISHED)
    ECHO -------------------------------------------------------------------------------
    echo.                         Repairing System Files
    ECHO -------------------------------------------------------------------------------
    dism /online /cleanup-image /restorehealth:wim:"%TEMP%\install.wim":1
    GOTO :FINISHED
    
    
    
    
    :FINISHED
    IF EXIST "%TEMP%\IMAGELIST.TXT" del /q/f "%TEMP%\IMAGELIST.TXT"
    IF EXIST "%TEMP%\install.wim" del /q/f "%TEMP%\install.wim"
    ECHO -------------------------------------------------------------------------------
    echo.                                 Finished
    ECHO -------------------------------------------------------------------------------
    echo Press any key to exit...
    pause>NUL
    goto :EOF
    I've attached the txt file. Rename the extension to .cmd to be able to run it.
     

    Attached Files:

  2. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    I will add that I tested tenforum's method of using esd file and it did not work.
    It's likely that they thought it worked because it reported success, but with actual broken files, neither split-wim or esd format worked.
    They have to be converted to a wim.
     
  3. Simpuhl

    Simpuhl MDL Senior Member

    Aug 8, 2012
    396
    43
    10
    #3 Simpuhl, Aug 14, 2015
    Last edited: Aug 14, 2015
    s**t... I opened it without administrative rights and got 1000000 powershell window popups lol

    Edit: Also it didnt detect my windows 10 usb stick... so im doing online mode
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    It doesn't loop the permissions raise. Me thinks you're being hyperbolic.
     
  5. Simpuhl

    Simpuhl MDL Senior Member

    Aug 8, 2012
    396
    43
    10
    ok it was more like 30 times.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  6. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    It only runs the elevation once.
     
  7. Simpuhl

    Simpuhl MDL Senior Member

    Aug 8, 2012
    396
    43
    10
    Would you like me to do it again and make a video so you don't think im making it up? What purpose what I have of doing that
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  8. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    You could if you wish. I'm just stating that it only makes the elevation call once, so it's not a problem with the script.
    You would likely have the same problem with any script trying to elevate permissions.
    You probably did something to UAC that is causing this.
     
  9. Simpuhl

    Simpuhl MDL Senior Member

    Aug 8, 2012
    396
    43
    10
    Uac is disabled. Anyway I ran the script with admin rights and it worked fine. I was hoping it would maybe fix my edge crashing and my wndows search but no luck. :( was worth a try
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  10. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    Those two things are windows apps. Windows apps have a known issue with uac mods.
    Did you use the UAC slider or did you reg mod the uac change?
     
  11. Simpuhl

    Simpuhl MDL Senior Member

    Aug 8, 2012
    396
    43
    10
    I am pretty sure I did it via slider but I cant recall now
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  12. Simpuhl

    Simpuhl MDL Senior Member

    Aug 8, 2012
    396
    43
    10
    #12 Simpuhl, Aug 14, 2015
    Last edited: Aug 14, 2015
    Microsoft Edge opens and closes after a few seconds.

    Windows Search keeps getting search ready... and won't pull any results from the control panel but it pulls other results.

    Also another issue is when I try to submit feedback with Windows Feedback it wants me to enable Diagnostic and usage data...

    When I change it to full or anything else it doesn't actually change it and goes back to blank thus not allowing me to submit any feedback. Its weird...

    I think one of the random tweaks I applied from the site screwed it up
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  13. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    Probably did. I'd say start over if you can't figure out how to reverse whatever is causing it.
    If I had to guess, I'd say some service somewhere was removed or disabled.
     
  14. Edwe

    Edwe MDL Member

    Sep 1, 2011
    152
    10
    10
    Hia

    I did a scannow yesterday and it found some errors. I am not comfortable with the use of the "dism" commands.
    Is this the script for me?
    I have the install files on a DVD, is that ok or should I copy it somewere else
     
  15. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    It should be fine, just mount the dvd or put it in the drive and run the script.
     
  16. janos666

    janos666 MDL Member

    Feb 25, 2012
    132
    22
    10
    #16 janos666, Aug 14, 2015
    Last edited: Aug 14, 2015
    I assume so but I am not entirely sure and wonder if SFC became completely redundant with the inception of DISM's RestoreHealth functionality.

    DISM has been working a lot better for me. It seems to be more thorough in checking for errors (might find additional errors which are not reported by SFC due to their intended scope, I presume, I think SFC is limited to the DLL store while RestoreHealth checks basically every system files) and a lot more reliable in fixing them (SFC might report some files to be unfixable or fixed but still broken after reboot and recheck which can be fully repaired by DISM).
    Yet, SFC is still present in Win10. I could understand if redundant tools coexisted in one or two releases for compatibility reasons but it's still here and not even "deprecated" now as far as I can tell (without explicitly searching for such info).

    The only thing I don't like about DISM is that it can rarely work from WU (despite a stable internet connection) and requires me to specify a custom local repair source which is up to date. It's rarely a problem for me because I usually have 2+ Windows PC at home, so I can always use a network share but it can be inconvenient in times like this when some of the machines are still on Win8 and the others are on Win10.
    Not that I need this tool every day. It's practically just the gaming PC which gets corrupted from time to time due to overclocking or beta software (and consequential system crashes + hard resets) and the lack of battery/UPS (and not so rare power outages).
     
  17. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
    dism has scanhealth and checkhealth. I've seen someone post that "sfc /scannow" shows errors when the others don't.
    I don't know how true that is. I do know that restorehealth has to have the ability to find the errors otherwise there's no way it could repair them.
    So yah, I'd say that dism can probably find the errors the same and the reports of sfc working better are false.
     
  18. Superfly

    Superfly MDL Expert

    Jan 12, 2010
    1,142
    543
    60
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  19. murphy78

    murphy78 MDL DISM Enthusiast

    Nov 18, 2012
    7,421
    11,692
    240
  20. Superfly

    Superfly MDL Expert

    Jan 12, 2010
    1,142
    543
    60
    OIC, Thanx - I think I'll upgrade that script as intended anyway.
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...