Programming Field

Reg Compare - DOS/Command Prompt Reference

[Windows NT series/XP or later] Compares the data contained within registry keys. This is one of the operations of Reg command.

Syntax

reg[.exe] compare <key1> <key2>
    [/oa | /od | /os | /on] [/v <var-name> | /ve] [/s] [/reg:[32|64]]

Options

<key1>, <key2>

Specifies a key name for each of them. For specific instructions on how to specify the key name, please refer to ‘About specifying key names’.

Both key names are specified with the complete path, and if either of the keys does not exist, an error will occur.

/oa | /od | /os | /on

Specifies the option to output the comparison result. Only one of them can be specified. If omitted, "/od" appears to be used.

Option(Origin)意味
/oaOutput AllOutputs both matching and non-matching contents.
/odOutput DiffOutputs all non-matching contents.
/osOutput SameOutputs all matching contents.
/onOutput NothingDoesn't output both matching and non-matching contents; instead, outputs only ‘Identical’ or ‘Different’ results.

* ‘The operation completed successfully’ will always be output. If you want to completely suppress the output, you need to use output redirection with ‘NUL’.
* This option needs to be specified before ‘/v’ or ‘/ve’.

/v <var-name> | /ve

If using ‘/v <var-name>’, specifies the name of the value you want to compare 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 compares the values of the ‘Default’ (or ‘Standard’, unnamed) value in the specified keys.

/s

<key1> and <key2> (hereafter referred to as parent keys) and all subkeys and values within them are considered as the comparison targets. If there are subkeys present in only one of the parent keys, those subkeys themselves are treated as ‘Different’, and the comparison for those subkeys and values is omitted.

This option cannot be used together with ‘/v’ or ‘/ve’.

/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 ‘COMPARE’ command in Reg is used to compare registry keys. You can perform comparisons on the specified two parent keys using the following patterns.

  1. Comparison of specific values -- executed by specifying ‘/v <var-name>’ or ‘/ve’
  2. Comparison of all values directly under the keys -- performed without specifying value-related options (/v and /ve) and without ‘/s’
  3. Comparison of all subkeys and values directly under the keys -- executed by specifying ‘/s’.

For value comparison, not only binary-level data comparison but also consideration of whether the value types match is taken into account. For example,

HKCU\Software\Hoge App
└ (val) Root = REG_SZ "D:\home"
HKCU\Software\Piyo App
└ (val) Root = REG_EXPAND_SZ "D:\home"

... in the comparison of these keys (‘HKCU\Software\Hoge App’ and ‘HKCU\Software\Piyo App’), both have a value named ‘Root’, but it is considered a ‘Different’ because the value types are different, even though the data may be the same.

If the output of the comparison result is specified with one of the options ‘/oa’, ‘/od’, or ‘/os’ (treated as ‘/od’ if omitted), specific comparison details corresponding to each option will be output. The comparison details are output one line at a time, accompanied by the following symbols at the beginning:

  • ‘=’ -- Matching content
  • ‘<’ -- Mismatch, content in <key1> if present
  • ‘>’ -- Mismatch, content in <key2> if present

* If the same-named value exists in both parent keys and their contents are mismatched, both contents will be output as ‘Different’ for each.
* Using the For command, it is possible to parse this content. However, please note that it may not be the most suitable output format for parsing, as distinguishing between space characters as part of key names or as delimiters can be challenging.

‘REG COMPARE’ command returns the following exit codes upon completion. You can use these exit codes with the If command or similar constructs in a batch file to implement conditional branching:

Exit codeMeaning
0Successful: Comparison result is a match
1Error exit (due to option misspecification or <key1> or <key2> not found, etc.)
2Successful: Comparison result is a mismatch

* Successful exit is indicated by ‘0’ and ‘2’, while error exit is indicated by ‘1’. Therefore, when comparing with ‘If Errorlevel’, be mindful of the numerical relationship.

Samples

Sample

reg compare "HKCU\Software\My Company\GoodApp" "HKLM\Backup-Data\My Company\GoodApp" /s

The sample compares all subkeys and values within the ‘HKEY_CURRENT_USER\Software\My Company\GoodApp’ key to the data in the ‘HKEY_LOCAL_MACHINE\Backup-Data\My Company\GoodApp’ key (subkeys are also checked due to the ‘/s’ option). This sample is designed for scenarios where registry data saved to the hive is loaded as ‘HKEY_LOCAL_MACHINE\Backup-Data’ and then compared with the existing content.