Skip to main content

How to Compare Two Registry Keys in Batch Script

Verifying system state, troubleshooting configuration changes, or ensuring that a software installation has been applied correctly often requires comparing parts of the Windows Registry. You might need to check a computer's live configuration against a "golden master" template to ensure compliance. For this task, Windows provides a powerful, built-in command: REG COMPARE.

This guide will teach you how to use the REG COMPARE command to compare two registry keys from a batch script. You will learn how to interpret its output and, more importantly, its exit codes (%ERRORLEVEL%) to programmatically determine if two keys are identical or different, a crucial skill for any administrative or diagnostic script.

The Core Command: REG COMPARE

The REG.EXE utility's COMPARE command is designed to compare all the values and subkeys under two specified registry keys. It is the definitive tool for this job.

Syntax: REG COMPARE <Key1> <Key2> [Options]

  • <Key1> and <Key2>: The full paths to the two registry keys you want to compare.
  • [Options]: Switches to control the comparison's behavior.

This command requires read access to both keys. If you are comparing keys in HKEY_LOCAL_MACHINE (HKLM), you will likely need to run your script as an administrator.

Understanding the Exit Codes for Scripting

For a script, the most important result of REG COMPARE is the %ERRORLEVEL% it sets.

  • 0: The keys are Identical.
  • 1: The operation Failed (e.g., syntax error, access denied, key not found).
  • 2: The keys are Different.

This three-way result is perfect for building intelligent logic in your scripts.

Basic Example: A Simple Key Comparison

This script will create two registry keys, make one value different, compare them, and then clean up.

@ECHO OFF
SET "Key1=HKCU\Software\MyApp\Original"
SET "Key2=HKCU\Software\MyApp\Modified"

ECHO --- Setting up test registry keys ---
REG ADD "%Key1%" /v Version /t REG_SZ /d "1.0" /f > NUL
REG ADD "%Key2%" /v Version /t REG_SZ /d "1.1" /f > NUL
ECHO.

ECHO --- Comparing the two keys ---
REG COMPARE "%Key1%" "%Key2%"

ECHO.
ECHO --- Cleaning up test keys ---
REG DELETE "HKCU\Software\MyApp" /f > NUL

The command provides a human-readable "diff" showing the differences it found, along with a summary.

--- Comparing the two keys ---
< Version REG_SZ 1.0
> Version REG_SZ 1.1

Result Compared: Different
The operation completed successfully.
note

Notice the final message is "The operation completed successfully" because the comparison itself worked. You must check the %ERRORLEVEL% to know the result of the comparison.

How to Use the Result in a Script (%ERRORLEVEL%)

This is how you automate the check. The script runs REG COMPARE silently and then inspects the exit code to determine the outcome.

@ECHO OFF
SET "Key1=HKCU\Software\MyApp\Original"
SET "Key2=HKCU\Software\MyApp\Modified"
REM (Assume keys are already created)

REM Run the comparison and redirect its output so the script is clean.
REG COMPARE "%Key1%" "%Key2%" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO [STATUS] The registry keys are identical.
) ELSE IF %ERRORLEVEL% EQU 2 (
ECHO [STATUS] The registry keys are DIFFERENT.
) ELSE (
ECHO [FAILURE] The comparison failed. Check for errors (e.g., key not found).
)

Key REG COMPARE Parameters Explained

  • /v <ValueName>: Compares a specific value within the keys, rather than all values.
  • /ve: Compares the value of the key's (Default) entry.
  • /s: Subkeys. Recursively compares all values and subkeys under the specified keys. This is essential for comparing an entire configuration branch.
  • /oa: Output All. Shows both matches and differences.
  • /od: Output Differences. This is the default.
  • /os: Output Successes. Shows only the matches.
  • /on: Output Nothing. Runs completely silently. This is a great alternative to > NUL.

Common Pitfalls and How to Solve Them

Problem: "Access is denied." (Administrator Privileges)

If you are comparing keys in a protected part of the registry like HKEY_LOCAL_MACHINE, you will need elevated rights.

Solution: The script must be run as an Administrator.

Problem: Comparing Keys on a Remote Computer

You may need to compare a key on your local machine to one on a remote server.

Solution: REG COMPARE supports this using a UNC-like syntax for the registry. REG COMPARE "\\RemotePC\HKLM\Software\MyApp" "HKLM\Software\MyApp" This command compares the MyApp key on RemotePC with the one on the local machine. You will need the appropriate administrative permissions on the remote computer for this to work.

Practical Example: A Configuration Auditor Script

This script checks if an application's live settings match a pre-defined "golden master" template stored elsewhere in the registry.

@ECHO OFF
SETLOCAL
TITLE Registry Configuration Auditor

SET "LiveKey=HKLM\SOFTWARE\MyCompany\WebApp\Settings"
SET "TemplateKey=HKLM\SOFTWARE\MyCompany\GoldenTemplates\WebApp_v1"

ECHO --- Auditing Application Configuration ---
ECHO Live Key: "%LiveKey%"
ECHO Template Key: "%TemplateKey%"
ECHO.

REM --- Run the comparison recursively and silently ---
REG COMPARE "%LiveKey%" "%TemplateKey%" /s /on

SET "ExitCode=%ERRORLEVEL%"

IF %ExitCode% EQU 0 (
ECHO [SUCCESS] The application's configuration is compliant with the template.
) ELSE IF %ExitCode% EQU 2 (
ECHO [FAILURE] The application's configuration is NON-COMPLIANT.
ECHO Run the following command to see the differences:
ECHO REG COMPARE "%LiveKey%" "%TemplateKey%" /s
) ELSE (
ECHO [ERROR] The audit failed to run. Check if keys exist and you are an Admin.
)

ENDLOCAL

Conclusion

The REG COMPARE command is a powerful and essential tool for any administrator who needs to verify registry settings from a script.

Key takeaways for using it effectively:

  • The command's exit code (%ERRORLEVEL%) is the key to automation: 0 means identical, 2 means different.
  • You must run as an Administrator when comparing keys in HKEY_LOCAL_MACHINE.
  • Use the /s switch for a recursive comparison of an entire key and its subkeys.
  • Use the /on switch or redirect output to > NUL for a silent check in your scripts.