.bat script registry query and change

The following script demonstrates how to query registry to check if specific registry key exists. If it does, it checks the value and if doesn’t match required it will remove and recreate with proper value.

————————————————————————————————–

@echo OFF
REM Checks if registry key exists -> If Exists and not equals 
REM to something -> change to something
REM Created: Dec 08, 2009
REM Author: Naz Snidanko

setlocal ENABLEEXTENSIONS
set KEY_NAME="HKEY_USERS\.DEFAULT\Control Panel\International"
set VALUE_NAME="sShortDate"
set SerVal=M/d/yyyy

REG QUERY %KEY_NAME% /v %VALUE_NAME%

if %errorlevel% EQU 0 goto Rexist
if %errorlevel% EQU 1 goto NotExist1

:NotExist1
echo Its not here!
goto :DoNe

:Rexist
echo Its Here!

FOR /F "usebackq skip=4 tokens=1-3" %%A IN (`REG QUERY %KEY_NAME% /v %VALUE_NAME% 2^>nul`) DO (

    set ValueName=%%A    
    set ValueType=%%B   
    set ValueValue=%%C
)

IF %ValueValue% NEQ %SerVal% GOTO Change

goto DoNe

:Change
REG DELETE %KEY_NAME% /v %VALUE_NAME% /f
REG ADD %KEY_NAME% /v %VALUE_NAME% /t REG_SZ /d %SerVal% /f
goto DoNe
:doNe

————————————————————————————————
Hope it helps!

This entry was posted in .bat scripts, Random stuff and tagged , , . Bookmark the permalink.

5 Responses to .bat script registry query and change

  1. Masum says:

    Hey, can you tell me, how can I change the winlogon shell value “explorer.exe” to other with one click, I mean with command or other batch progarm.
    Plz, give the script..
    Plz..plz… I badly need that….

  2. Masum says:

    Plz, give me a reply ASAP…!!

  3. Naz says:

    Hi Masum,

    I dont have XP system around to test it.
    Just replace variables in my script with the following:

    set KEY_NAME=”HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon”
    set VALUE_NAME=”Shell”
    set SerVal=explorer.exe

    Don’t forget to test it before deploying live.

  4. Mr. Lee says:

    How can I modify this to search for multiple registry keys and change the value?

    • Naz says:

      You simply need to define additional variable for another registry keys:
      set KEY_NAME2
      set VALUE_NAME2
      set SerVal2

      and use this loop to check
      FOR /F “usebackq skip=4 tokens=1-3” %%A IN (`REG QUERY %KEY_NAME2% /v %VALUE_NAME2% 2^>nul`) DO (

      set ValueName=%%A
      set ValueType=%%B
      set ValueValue=%%C
      )

      IF %ValueValue% NEQ %SerVal2% GOTO Change

Leave a Reply

Your email address will not be published. Required fields are marked *