Tip 116: Manipulate the Windows Registry from the command line

If you want to manipulate the Windows Registry from the command line, then Reg.exe is your tool.
This tool ships with Windows XP (and also comes as part of the Windows 2000 resource kits).

The general syntax for Reg.exe is:

REG Operation [Parameter List]

where Operation can be:

  QUERY ADD DELETE
  COPY SAVE LOAD
  UNLOAD RESTORE COMPARE
  EXPORT IMPORT

To find out about the syntax of [Parameter List], at the command prompt type:

reg [operation] /?

Some cases require you to take some precautions when passing special characters as values.
To pass a value that contains quotes, you must add a slash (\) before each quote as an escape sequence.

For example,

H:\>reg add HKLM\Software\sav /v test /t REG_SZ /d "%userprofile%"

adds the user profile value without the quotes. However,

H:\>reg add HKLM\Software\sav /v test /t REG_SZ /d "\"%userprofile%\""

maintains the quotes around the user profile value.

For example, for user profile MyUserName, including the slashes set the registry value to
"H:\Documents and Settings\MyUserName"
rather than just >br> H:\Documents and Settings\MyUserName.

In the same way you can use a slash (\) as an escape character to pass a double quote ("), you can use a caret (^) as an escape character to pass a percent sign (%).

For example

H:\>reg add HKLM\Software\sav /v test /t REG_SZ /d "\"%userprofile^%\""

maintains the user-profile value’s percent sign.

Without the caret, the reg.exe tool will evaluate the value between the percent signs.

With the caret, reg.exe will leave the value unchanged.