Adding and editing (importing) registry entries
Adding items to the registry requires a *.REG file:
REGEDIT [ /S ] addsome.REG
The /S swith is optional, it skips the message dialogs before and after the import of the *.REG file.
Since NT 4 .REG files are in readable ASCII, they may be created “on the fly” by our batch files.
This is demonstrated in the DefOpen example in the Examples section.
Removing registry entries
To remove an entire “tree” from the registry using REGEDIT and a .REG file, just add a minus sign before the tree name:
will remove the entire tree “DummyTree”.
To remove an individual item from the registry, place the minus sign after the equal sign:
REGEDIT4 [HKEY_CURRENT_USER\DummyTree] "ValueToBeRemoved"=-
will remove the individual value “ValueToBeRemoved” from “DummyTree”.
This is demonstrated in the uniqueid.bat example below, a batch file that forces a new LANDesk agent ID.
Reading (exporting) from the registry
REGEDIT’s /E switch can be used to export a registry key:
REGEDIT /E d:\path\filename.REG "HKEY_XXXX\Whatever Key"
This will write the registry key “HKEY_XXXX\Whatever Key” and its subkeys to a file named d:\path\filename.REG
The resulting (ASCII or UniCode) file will contain the entries in the format
"key"="value", which can be stripped and parsed using Laurence Soucy‘s CHOICE trick (How-to #4, second method) for MS-DOS 6 and Windows 9*, NT’s FOR /F or the more generic TYPE and FIND commands.
Instead of a file name, some device names can be used:
REGEDIT /E PRN "HKEY_XXXX\Whatever Key"
will print the selected key.
Unfortunately, this won’t work for CON (console or display).
Self-contained registry scripts
In .REG files, every line after the first one that starts with a semicolon (;) is treated as comment.
Batch files completely ignore the semicolons before commands.
So the following batch file will use itself as a .REG file:
REGEDIT4 ; @ECHO OFF ; CLS ; REGEDIT.EXE /S "%~f0" ; EXIT [HKEY_CURRENT_USER\Software\Test] "TestVal"="Succeeded"
REGEDIT4 line is required, and must be the first line, otherwise
REGEDIT.EXE won’t accept the script as a valid .REG file. However, it will generate an error message when running as a batch file, hence the
CLS command to wipe the error message from the screen.
To prevent more error messages,
EXIT is used to abort the batch file immediately after the
Use this technique to add or remove registry keys and values.