This will run the commands and inform you that the commands were run (with a status message)
Open a log file at the end for confirmation.
- dism check health
- dism scan health
- dism restore health (if needed)
- sfc /scannow
- It
- Elevates privileges using VBScript if the script isn't already running as administrator.
- Sets up logging to record all actions, successes, failures, and skipped commands in %temp%\script_log.txt.
- Runs dism /Online /Cleanup-Image /CheckHealth to determine if there are any system integrity issues.
- Conditionally runs dism /Online /Cleanup-Image /RestoreHealth only if corruption is found.
- Logs and displays skipped commands (e.g., RestoreHealth if no corruption is detected).
- Executes sfc /scannow at the end to scan and repair system files.
- Logs success or failure messages for every command executed.
- Opens the log file automatically in Notepad after execution for easy review.
- Pauses execution at the end, allowing the user to review the command window before it closes.
Content of batch file:
Code:
@ECHO OFF
setlocal EnableDelayedExpansion
:: Elevation check
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto START ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' ( goto START )
set "batchPath=%~f0"
set "batchArgs=ELEV"
set "script=%0"
set script=%script:"=%
IF '%0'=='!script!' ( GOTO PathQuotesDone )
set "batchPath=""%batchPath%"""
:PathQuotesDone
:ArgLoop
IF '%1'=='' ( GOTO EndArgLoop ) else ( GOTO AddArg )
:AddArg
set "arg=%1"
set arg=%arg:"=%
IF '%1'=='!arg!' ( GOTO NoQuotes )
set "batchArgs=%batchArgs% "%1""
GOTO QuotesDone
:NoQuotes
set "batchArgs=%batchArgs% %1"
:QuotesDone
shift
GOTO ArgLoop
:EndArgLoop
:: Create and run VBScript for elevation
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%temp%\OEgetPrivileges.vbs"
ECHO UAC.ShellExecute "cmd", "/c ""!batchPath! !batchArgs!""", "", "runas", 1 >> "%temp%\OEgetPrivileges.vbs"
"%temp%\OEgetPrivileges.vbs"
exit /B
:START
:: Remove elevation flag and set working directory
IF '%1'=='ELEV' ( shift /1 )
cd /d %~dp0
:: Begin cleanup script with logging
set "logFile=%temp%\script_log.txt"
echo Cleanup process started: %DATE% %TIME% > "%logFile%"
echo Cleanup process started: %DATE% %TIME%
call :RunCommand "dism /Online /Cleanup-Image /CheckHealth"
if %errorlevel% NEQ 0 (
echo Issues detected in Windows image, running RestoreHealth... >> "%logFile%"
echo Issues detected in Windows image, running RestoreHealth...
call :RunCommand "dism /Online /Cleanup-Image /RestoreHealth"
) else (
echo No corruption found, skipping RestoreHealth. >> "%logFile%"
echo No corruption found, skipping RestoreHealth.
echo SKIPPED: dism /Online /Cleanup-Image /RestoreHealth >> "%logFile%"
echo SKIPPED: dism /Online /Cleanup-Image /RestoreHealth
)
:: Run SFC only once at the end
call :RunCommand "sfc /scannow"
echo Cleanup completed successfully: %DATE% %TIME% >> "%logFile%"
echo Cleanup completed successfully: %DATE% %TIME%
:: Open log file at the end
start notepad "%logFile%"
pause
exit /B
:RunCommand
echo Running: %~1
%~1
if %errorlevel% EQU 0 (
echo %~1 Completed successfully. >> "%logFile%"
echo %~1 Completed successfully.
) else (
echo ERROR: %~1 failed with error level %errorlevel% >> "%logFile%"
echo ERROR: %~1 failed with error level %errorlevel%.
)
exit /B
If you don't want the log file to open at the end, say so
Log file confirmation on completion will look like this:
Command window looks like this on completion: