Skip to content

Instantly share code, notes, and snippets.

@theorium-0
Created June 18, 2025 10:02
Show Gist options
  • Save theorium-0/8e06c304cfc0476e3ea90b51cc28cf42 to your computer and use it in GitHub Desktop.
Save theorium-0/8e06c304cfc0476e3ea90b51cc28cf42 to your computer and use it in GitHub Desktop.
Repairs a broken Windows PC by means of resetting Group Policy, WMI, and restarting services.
@echo off
setlocal enableextensions enabledelayedexpansion
cls
:: Check for administrative privileges
NET SESSION >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (
echo Requesting administrative privileges
goto :elevate
) ELSE (
echo This script is running as "%COMPUTERNAME%\%USERNAME%"
)
:: --- Stage 1: Resetting Core Configuration ---
echo. & echo --- Stage 1: Resetting Core Configuration ---
echo Resetting Local Group Policy settings...
RD /S /Q "%WinDir%\System32\GroupPolicyUsers" >NUL 2>&1
RD /S /Q "%WinDir%\System32\GroupPolicy" >NUL 2>&1
gpupdate /force
echo Group Policy reset and updated COMPLETE.
:: --- Stage 1.1: Rebuilding WMI Repository ---
echo. & echo --- Stage 1.1: Rebuilding WMI Repository ---
echo Attempting to terminate the WMI service...
net stop Winmgmt /y >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (
echo Initial attempt to stop WMI service FAILED.
echo Attempting to terminate related WMI processes (wmiprvse.exe, winmgmt.exe)
taskkill /f /im wmiprvse.exe >NUL 2>&1
taskkill /f /im winmgmt.exe >NUL 2>&1
timeout /T 2 >NUL 2>&1
net stop Winmgmt /y >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (
echo FAILED to terminate WMI service. Cannot proceed with repository rename.
goto :end_wmi_rebuild_error
)
)
echo WMI service stopped COMPLETE.
timeout /T 3 >NUL 2>&1
:: 2. Rename the old repository
echo. & echo Attempting to rename WMI repository...
cd /d "%systemroot%\System32\wbem\repository.old"
IF exist %cd% (
IF exist "%systemroot%\System32\wbem\repository" (
echo Removing old WMI repository.
cd /d "%systemroot%\System32\wbem\repository.old"
IF exist %cd% (
rmdir /S /q "%systemroot%\System32\wbem\repository.old"
)
) ELSE (
echo "%systemroot%\System32\wbem\repository.old" is the only WMI repository found on "%systemroot%".
echo Renaming to "repository"
ren "%systemroot%\System32\wbem\repository.old" repository
)
)
ren "%systemroot%\System32\wbem\repository" repository.old
IF %ERRORLEVEL% NEQ 0 (
echo Rename WMI repository FAILED.
echo This usually means the repository is locked or corrupted.
echo Please check "%systemroot%\System32\wbem\repository" manually after the script finishes.
goto :end_wmi_rebuild_error
)
echo Rename WMI repository to "repository.old" COMPLETE.
:: 3. Start WMI Service (this will create a new, empty repository)
echo.
echo Attempting to start the WMI service...
net start Winmgmt >NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (
echo FAILED to start the WMI service. Cannot proceed with registration.
goto :end_wmi_rebuild_error
)
echo WMI service started and new repository created COMPLETE.
timeout /T 5 >NUL 2>&1
:: --- Stage 1.2: Re-registering WMI Components ---
echo.
echo --- Stage 1.2: Re-registering WMI Components ---
cd /d "%windir%\System32\wbem"
if not exist "%cd%" (
echo FAILED to find WMI directory "%windir%\System32\wbem" or inaccessible.
echo Cannot proceed with WMI component registration.
goto :end_wmi_rebuild_error
)
echo Current directory for WMI registration: %cd%
echo.
echo Registering WMI DLLs...
for /f "delims=" %%i in ('dir /b *.dll 2^>nul') do (
regsvr32 /s "%%i" >NUL 2>&1
if %ERRORLEVEL% NEQ 0 (
echo FAILED to register DLL: %%i
)
)
echo DLL registration attempt COMPLETE.
:: The problematic EXE loop has been removed as it can cause GUI applications like WMI Tester to open.
:: for /f "delims=" %%i in ('dir /b *.exe 2^>nul') do (
:: echo Registering EXE: %%i
:: %%i /regserver >NUL 2>&1
:: )
echo Compiling WMI MOF files...
for /f "delims=" %%i in ('dir /b *.mof 2^>nul') do (
mofcomp "%%i" >NUL 2>&1
if %ERRORLEVEL% NEQ 0 (
echo FAILED to compile MOF: %%i
)
)
echo MOF compilation attempt COMPLETE.
goto :continue_script
:end_wmi_rebuild_error
echo.
echo WMI Rebuild and/or Registration FAILED during one or more critical steps.
echo WMI may still be broken. Proceeding with other steps but investigate WMI further.
echo.
pause
:: --- Stage 2: System File and Image Repair ---
:continue_script
echo.
echo --- Stage 2: System File and Image Repair ---
echo Running DISM to repair OS image...
DISM /Online /Cleanup-Image /RestoreHealth
echo DISM scan COMPLETE.
echo Running SFC to verify system files...
sfc /scannow
echo SFC scan COMPLETE.
:: --- Stage 3: Disk Check (Requires Restart) ---
echo.
echo --- Stage 3: Disk Check (Requires Restart) ---
echo Scheduling disk check (chkdsk) for next restart...
chkdsk C: /f /r
:: --- Stage 4: Final Steps ---
echo.
echo --- Stage 4: Final Steps ---
echo Restarting Windows Explorer...
taskkill /F /IM explorer.exe
timeout /T 2 >NUL 2>&1
start explorer.exe
echo File Explorer reload COMPLETE.
:: Restart the computer (recommended to apply all changes)
echo.
echo All repairs initiated. Restarting computer to apply changes...
shutdown /r /f /t 5
goto :eof
:elevate
:: Embed and execute the VBScript directly using cscript/wscript
:: The batch file will be executed as a VBScript, and then that VBScript
:: will launch the batch file again as administrator.
:: Execute the batch file itself as a VBScript, telling it to elevate
cscript //nologo "%~f0" "VBSCRIPT_ELEVATE"
exit /b
:: --- VBScript Block (must be at the end of the batch file) ---
:: This part is ignored by CMD but executed by cscript/wscript
<nul>
@goto :EOF
'
' This is the VBScript portion.
' It will only be executed when the batch file is run by cscript/wscript
' (i.e., during the elevation step).
'
If WScript.Arguments.Count > 0 Then
If WScript.Arguments(0) = "VBSCRIPT_ELEVATE" Then
' Get the path to the current batch file
Dim objShell, strScriptPath
Set objShell = CreateObject("Shell.Application")
strScriptPath = WScript.ScriptFullName
' Run the batch file again as administrator
' The "" at the end ensures no arguments are passed to the elevated instance,
' so it doesn't get stuck in an endless elevation loop.
objShell.ShellExecute strScriptPath, "", "", "runas", 1
WScript.Quit
End If
End If
' End of VBScript block
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment