Skip to content

Instantly share code, notes, and snippets.

@mxxcon
Created September 13, 2025 21:06
Show Gist options
  • Select an option

  • Save mxxcon/d18c89561c7ab14be4dd8966ea246862 to your computer and use it in GitHub Desktop.

Select an option

Save mxxcon/d18c89561c7ab14be4dd8966ea246862 to your computer and use it in GitHub Desktop.
@echo off
:: ============================================================================
:: Safer WMI Repair and Diagnostic Script
:: Version 2.0
::
:: This script provides an interactive menu to diagnose and repair the
:: Windows Management Instrumentation (WMI) repository. It follows a
:: phased approach from least to most invasive actions.
::
:: IMPORTANT: This script should be run with elevated privileges
:: ("Run as Administrator").
:: ============================================================================
:MENU
cls
echo.
echo ====================================================================
echo Interactive WMI Repair and Diagnostic Tool
echo ====================================================================
echo.
echo Please select an option:
echo.
echo --- Diagnostics ---
echo [1] Verify WMI Repository Consistency
echo.
echo --- Repair Options (Least to Most Destructive) ---
echo [2] Attempt Salvage Repository (Recommended First Step)
echo [3] Re-register WMI Components and Recompile MOFs
echo [4] Reset WMI Repository to OS Defaults (LAST RESORT)
echo.
echo --- Other ---
echo [5] Exit
echo.
echo ====================================================================
echo.
set /p choice="Enter your choice [1-5]: "
if "%choice%"=="1" goto DIAGNOSE
if "%choice%"=="2" goto SALVAGE
if "%choice%"=="3" goto REBUILD
if "%choice%"=="4" goto RESET_CONFIRM
if "%choice%"=="5" goto EXIT
echo Invalid choice. Please try again.
pause
goto MENU
:DIAGNOSE
cls
echo.
echo --- [1] Verifying WMI Repository Consistency ---
echo.
echo This command performs a consistency check on the live WMI repository.
echo.
winmgmt /verifyrepository
echo.
echo --------------------------------------------------------------------
echo.
pause
goto MENU
:SALVAGE
cls
echo.
echo --- [2] Attempting to Salvage WMI Repository ---
echo.
echo This is the recommended first repair step. It performs a consistency
echo check and, if an inconsistency is found, rebuilds the repository
echo while attempting to merge data from the old repository.
echo.
echo Stopping the WMI service and its dependencies...
net stop winmgmt /y
echo.
echo Running the salvage operation...
winmgmt /salvagerepository
echo.
echo Restarting the WMI service...
net start winmgmt
echo.
echo --- Verifying the result of the salvage operation ---
echo.
winmgmt /verifyrepository
echo.
echo --------------------------------------------------------------------
echo If the repository is now consistent, test your applications.
echo If the issue persists, consider option.[3]
echo.
pause
goto MENU
:REBUILD
cls
echo.
echo --- [3] Re-registering WMI Components and Recompiling MOFs ---
echo.
echo This is an aggressive repair. It will stop the WMI service,
echo re-register all WMI-related DLLs, and recompile all standard
echo MOF files in the WBEM directory.
echo.
set /p confirm_rebuild="Are you sure you want to continue? (Y/N): "
if /i not "%confirm_rebuild%"=="Y" goto MENU
echo.
echo Disabling and stopping the WMI service...
sc config winmgmt start= disabled
net stop winmgmt /y
echo.
echo Changing directory to the WBEM folder...
cd /d %windir%\system32\wbem
echo.
echo Re-registering all provider DLLs (this may take a moment)...
:: This loop uses corrected syntax to iterate through all DLLs.
for /f %%s in ('dir /b *.dll') do (
echo Registering %%s...
regsvr32 /s %%s
)
echo DLL registration complete.
echo.
echo Recompiling MOF and MFL files (excluding uninstallers)...
:: This command creates a list of MOF/MFL files, filters out any
:: containing "uninstall", and then compiles the files from that list.
dir /b *.mof *.mfl | findstr /v /i "uninstall" > moflist.txt
for /f %%s in (moflist.txt) do (
echo Compiling %%s...
mofcomp %%s
)
del moflist.txt
echo MOF compilation complete.
echo.
echo Re-enabling and starting the WMI service...
sc config winmgmt start= auto
net start winmgmt
echo.
echo --------------------------------------------------------------------
echo The rebuild process is complete. A system reboot is recommended.
echo.
pause
goto MENU
:RESET_CONFIRM
cls
echo.
echo!!! --- [4] WARNING: DESTRUCTIVE OPERATION AHEAD ---!!!
echo.
echo You have selected to RESET the WMI Repository. This is a
echo "last resort" option that will delete the current repository
echo and restore it to the state it was in when the OS was first
echo installed.
echo.
echo POTENTIAL CONSEQUENCES:
echo - Third-party applications that use WMI (e.g., antivirus,
echo backup agents, monitoring software) will likely break.
echo - A full reinstallation of affected software may be required.
echo - All custom WMI configurations and data will be lost.
echo.
echo ====================================================================
echo.
set /p confirm_reset="Are you absolutely sure you wish to proceed? (Y/N): "
if /i not "%confirm_reset%"=="Y" goto MENU
goto RESET_EXECUTE
:RESET_EXECUTE
cls
echo.
echo --- Executing WMI Repository Reset ---
echo.
echo Stopping the WMI service and its dependencies...
net stop winmgmt /y
echo.
echo Resetting the repository...
winmgmt /resetrepository
echo.
echo Restarting the WMI service...
net start winmgmt
echo.
echo --- Verifying the result of the reset operation ---
echo.
winmgmt /verifyrepository
echo.
echo --------------------------------------------------------------------
echo The WMI repository has been reset to its initial state.
echo A SYSTEM REBOOT IS STRONGLY RECOMMENDED.
echo You must now test all third-party applications and reinstall
echo any that are no longer functioning correctly.
echo.
pause
goto MENU
:EXIT
cls
echo Exiting the script.
exit /b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment