Programming Field

Reg Query - DOS/Command Prompt Reference

[Windows NT series/XP or later] Searches and retrieves (queries) registry keys and values, and outputs the results. This is one of the operations of Reg command.

Syntax

reg[.exe] query <key> [/v <var-name> | /ve] [/s]
    [/f <data> [/k] [/d] [/c] [/e]] [/t <type>]
    [/z] [/se <separator>] [/reg:[32|64]]

Options

<key> Specifies the key name. For specific instructions on how to specify the key name, please refer to ‘About specifying key names’.
/v <var-name> | /ve

If using ‘/v <var-name>’, specifies the name of the value you want to retrieve for <var-name>. The name must be a standalone name and cannot include a pathname (the path should be included in <key>).

If using ‘/ve’, Reg retrieves the value of the ‘Default’ (or ‘Standard’, unnamed) value in the specified key.

/s When searching (retrieving) values or keys, if there are subkeys within <key>, those subkeys (and all their subcontents) are also included in the search scope.
/f <data> [/k] [/d]

Searches for values using <data>. The search scope is determined by the specified options as follows:

Specified optionSearch scope
/k onlyKey names that contain a part of <data>
/d onlyValues that contain a part of <data> in their data
/k and /d specifiedKey names that contain a part of <data> and values that contain a part of <data> in their data
/k and /d not specifiedKey names that contain a part of <data> and values that have <data> as part of their name or data

If the /e option is not used, you can use wildcard syntax with <data>. However, it will only be a partial match, so you cannot specify conditions like ‘starts with a specific character’.

/c
When specifying a string for <data>, searches with case sensitivity if you provide uppercase or lowercase characters. If not specified, it searches without case sensitivity.
* Registry typically does not distinguish between uppercase and lowercase letters in key names and value names.
/e During the search, only those that exactly match <data> will be considered hits. (The previous ‘have <data> as a part of xxx’ now means ‘matches <data>’).
/t <type>

Specifies (restricts) the type of values to search for. <type> will be the value type. Please refer to ‘Value types’ for the available options.

Note that when this option is specified, if only ‘/f <data>’ is specified without ‘/k’ and ‘/d’, it searches for values that include <data> (exact match if ‘/e’ is present). If ‘/t’ is present and ‘/f <data>’ is specified with either ‘/k’ or ‘/d’, the effects of ‘/f’, ‘/k’, and ‘/d’ are nullified (resulting in the same effect as not specifying ‘/f’).

* If ‘/t’ is present and a match is found in the value data with ‘/f <data>’, the number of matches is unexpectedly doubled. Due to this, there may be some bugs in the behavior when both ‘/t’ and ‘/f’ are included.

/z When displaying the list of values, it also shows the numeric value corresponding to the type of each value.
/se <separator>

Specifies the character to use as a separator when displaying a value with the type ‘REG_MULTI_SZ’ (7). <separator> will be a single character.

If this option is not specified, it uses the two characters ‘\0’ as the separator (‘\0’ cannot be specified for <separator>).

/reg:[32|64]

If there is an environment with both 64-bit and 32-bit versions (WOW64), specifying ‘/reg:32’ will reference registry data used by 32-bit applications (for example, ‘HKLM\Software\WOW6432Node’ for HKLM), and specifying ‘/reg:64’ will reference registry data used by 64-bit applications.

If this option is omitted, the executed Reg.exe will reference the registry corresponding to its bit version. If it is a 64-bit version, it will reference the 64-bit registry; if it is a 32-bit version (located under %SystemRoot%\SysWOW64), it will reference the 32-bit registry. If you want to reference registry data for a specific application and know whether it's 32-bit or 64-bit, you can explicitly specify this option to avoid ambiguity.

* Even if Reg.exe is a 32-bit version, you can reference the 64-bit registry by using ‘/reg:64’.
* If you are using the 64-bit version of Reg.exe, you can also directly specify keys such as ‘HKLM\Software\WOW6432Node’ when referencing the 32-bit registry.

Details

The ‘QUERY’ command in Reg is used to display, retrieve, or search for values within the registry. When you simply execute ‘reg query’ in the command line, it only outputs the information of values to the screen, and using a registry editor may be more convenient for checking. However, combining ‘reg query’ with the For command and its extended syntax allows you to store registry values in environment variables, making it a common choice when utilizing registry values in batch processing.

Note that the ‘QUERY’ command does not modify or delete registry keys or values.

Samples

Sample 1

reg query "HKCU\Software\My Company\MyApp" /v Version

Outputs the value ‘Version’ within the ‘HKEY_CURRENT_USER\Software\My Company\MyApp’ key. If the value is found, the output will be something like the following:

HKEY_CURRENT_USER\Software\My Company\MyApp
    Version    REG_SZ    1.1.0.27

Sample 2 (Batch file)

for /f "tokens=1,2*" %%1 in ('reg query "HKCU\Software\My Company\MyApp" /v Version') do if "%%1"=="Version" set APP_VERSION=%%3

[Extensions] Using the For command, it sets the retrieved value, similar to Sample 1, into the environment variable ‘APP_VERSION’.

* The output of ‘reg query’ spans multiple lines, so it is necessary to use the If command to extract the value from the line where the first token is "Version." Additionally, since the extracted value may contain spaces, the ‘tokens’ specification in For requires the use of ‘*’ to represent ‘the rest of the line’.
* If the value's name contains spaces, the tokens might be split at those spaces, making the above syntax unsuitable.

See also