Writing a value into the registry
The
values below a key can also be created with
reg /add
. Additionally, the name of the value needs to be specified with the
/v
option, its value with the
/d
option and the data type with the
/t
option.
If the value already exists and should be overwritten, the /f
(= force) option is also needed:
c:\> reg add HKCU\path\to\key /v nameOfValue /t dataType /d value /f
dataType is one of
-
REG_DWORD
-
REG_QWORD
-
REG_SZ
-
REG_MULTI_SZ
(which seems to be the utf-16 / Unicode variant of REG_SZ
)
-
REG_EXPAND_SZ
-
REG_BINARY
-
REG_NONE
(?)
In order to add a key's default value, the /ve
(e for empty value name) must be passed:
reg add HKCU\path\to\key /ve /t REG_DWORD /d 42
Querying a specific value in the registry
reg query
in combination with
/v
can be used to query for a specific value under a specific key in the
registry.
reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH
Unfortunately,
reg query
also prints the key and data type of the queried value. In order to only print the value, the
reg query
might be combined with
for /f "tokens=…"
to extract the value only. On the command line, that would be:
for /f "tokens=2,* skip=2" %a in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v TEMP') do echo %b
If this construct is used in a
batch file, the percent signs need to be doubled (
%%a
and
%%b
instead of
%a
and
%b
), see for example
regQuery.bat
.
Recursively search for a specific values
reg query … /s
allows to recursively search for a specific value under a given key.
The following command searches for any key (
/k
) under
HKLM\SOFTWARE\Classes
whose name contains
key-value.
reg query HKLM\SOFTWARE\Classes /k /f key-value
If the search is to be restricted to values that contain a specific value, /d
instead of /k
should be used.
The asterisk (star) is recognized as a wild card.
C:\> reg query HKEY_CURRENT_USER\AppEvents\Schemes /s /d /f "*.wav"