Registry Editing by Batch-Script (Microsoft Windows)

From MakeItFit - Wiki
Jump to: navigation, search

Valid at least for Windows XP.

In some cases certain settings stored in the Windows Registry (german article) are changed automatically repeated times, e.g. by programs or startup / login scripts. Changing them manually back to what is required or wished, is pretty annoying and often is not acceptable as final solution. One possibility is setting them back to the wished value automatically by a batch script runned as autostart object. I have a script running on my companies computer for example, to set the screen saver and desktop background to customized values.

Especially in cases where the user is not the systems administrator, one must make sure that the changes are in agreement to the policies of the organization or company. I do not take over any responsibilty for system failures or conflict with any policies and rules. This is always in the script authors responsbility.

Reference

How to make it

Easiest way to get the path and registry key you want to change into a text file usable for a batch script, is setting it to the wished value and export them from the Windows registry editor. To open the Windows registry editor use the command line or the run command in the Windows start menu and type

regedit

To export a certian key, select it and use the menu command

menu -> file -> export...

It will ask for a place and name to save the key into a .reg-file. This is a normal text file, the content can directly be used to write a script. The script can be written with any text editor and needs to be stored as text file, .txt. Afterwards the file type needs to be changed to .bat to make it an executable file.

Concept

Of course with this solution only registry entries can be modified one has the access privilidges for. Means, if one cannot change a certain setting, it is also not possible to change that with this batch script solution.

Comment:

  • Before registry entries are modified anyhow, check first if you are allowed to modify them, and if the achived setting is in accordance with the plicies.

Goal

The script has to run in based on certain boundary conditions and should fulfill certain comfort requirements. These are:

  1. run without bugging the user with any message demanding his interaction
  2. give adequate feedback so that the user recognizes if somethings wrong or the script does not work any more
  3. run at the right moment so that the commands achieve the changes and are not outdone by the startup/login script or the program

With the here presented methode these goals could be achieved not perfectly, but in an acceptable way. The script does not bug the user, but gives out an error message in the command line window and continues and stops at the respectivelly.

Functions

Export Registry Entry

To check the value of a certain registry entry it is necessary to read it from the registry. For the screensaver setting this is done by

REG QUERY "<registry path>" /v <registry key>

Comment:

  • /v specifies the registry key

Write Registry Entry

This is the most important step of the script - write the wished value into the registry:

reg add "<registry path>" /f /v <registry key> /d <value of the key>

Comment:

  • /f makes the command writing the new value without asking for confirmation

Wait a moment...

There is no real wait command for batch scripts in Windows XP, probably it exists on for newer releases of Windows. Instead installing an additonal tool, pinging the loopback network interface can be missused therefore. It takes a moment till the command finishs what causes the script to wait for about a second.

ping 127.0.0.1 > nul

Comment:

  • > nul causes the output to be sent to nirvana as it will not be used.

Wait for the right moment to run

Because our script should run after the unwished settings are written to the registry, the unwished value can be taken as a "start signal" for our script. Hence, find a registry entry which is changed by to the unwished value and compare it with a reference. If it is the unwished value, continue the script, otherwise wait:

:start
ping 127.0.0.1 > nul
FOR /F "SKIP=2 USEBACKQ TOKENS=3" %%i IN (`REG QUERY "<registry path>" /v <registry key>`) DO SET ScreenSaver="%%i" 
IF NOT %ScreenSaver%=="<unwished key value>" goto change
if %a% == 10 (goto change)
set /a a = a + 1
echo ...
goto start

:change
...

Comment:

  • If the script or program setting the unwished value does not run or not set the value for any reason, our script shall not wait endless but run after a sufficient long waiting time. Here it is set to 10 loops of pinging the loopbacknetwork, what is 10 seconds approximately.

Make the new registry values becoming active

As the registry setting are read at startup/logon, the changed values must be read newly. Otherwise all the work is useless. Hence, the last command of the script must be:

RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters

Comment:

  • I am not sure what parts are all read newly with the settign UpdatePerUserSystemParameters. It works fine for my us of changing the screensaver, password request after standby or screen saver and desktop background.

Example

My script executes the following changes / steps:

  • wait until the screensaver is set ot the companies default screensaver by the companies start script
  • change desktop background and the display mode of the background to centered
  • change the screen saver to blank screen
  • change the time when the screen saver starts
  • activate password request after reactivating the computer from screen saver or standby mode
  • stop the Windows help service (not needed if Windows help system is not used)
@ echo off
echo --- My Startscript ---
echo.
echo.
set /a a = 0
echo Bildschirmschoner auf Blankscreen setzen...
:start
ping 127.0.0.1 > nul
FOR /F "SKIP=2 USEBACKQ TOKENS=3" %%i IN (`REG QUERY "HKEY_CURRENT_USER\Control Panel\Desktop" /v SCRNSAVE.exe`) DO SET ScreenSaver="%%i" 
IF NOT %ScreenSaver%=="C:\\WINDOWS\\system32\\scrnsave.scr" goto change
if %a% == 10 (goto change)
set /a a = a + 1
echo ...
goto start

:change
:: Bildschirmhintergrund und Anzeigemodus setzen
echo Bildschirmhintergrund setzen
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /f /v Wallpaper /d "C:\Documents and Settings\username\My Documents\My Pictures\Wallpaper_Philipp.bmp"
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /f /v WallpaperStyle /d 0
echo.
echo.
:: Bildschirmschoner definieren, /d "<Pfad>", scrnsave.scr: Bildschirmschoner Blank
echo Bildschirmschoner setzen
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /f /v SCRNSAVE.EXE /d C:\\WINDOWS\\system32\\scrnsave.scr
echo.
echo.

:: Zeit in Sekunden, nach welcher der Bildschirmschoner einschaltet, /d xxx  
echo Zeit bis zum Bildschirmschoner setzen
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /f /v ScreenSaveTimeout /d 600
echo.
echo.

:: Passwortabfrage bei Reaktivieren nach Bildschirmschoner /d 1: ja, /d 0: nein 
echo Passwortabfrage nach Reaktivieren setzen
reg add "HKEY_CURRENT_USER\Control Panel\Desktop" /f /v ScreenSaverIsSecure /d 1
echo.
echo.
RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters
echo.
echo.


echo Windows Hilfsdienst stoppen...
net stop "Help and Support"
:: echo.
:: echo.
echo --- Fertig! ---