Checking if a key is present on the windows registry through batch file

37,459

How do I check if a key is present in the Windows registry?

This can be done using reg query key:

  • This command will set %errorlevel%.

  • errorlevel=0 means the key exists.

  • errorlevel=1 means the key does not exist.


How do I add a key to the windows registry?

To add a key if it is not present use reg add key.


Example batch file

@echo off
reg query mykey >nul
if %errorlevel% equ 0 (
  echo "mykey exists- do nothing"
) else (
  echo "mykey does not exist - add the key and give it a value"
  reg add mykey
  reg add mykey /v value ...
)

Further reading

Share:
37,459

Related videos on Youtube

Vinícius Simões
Author by

Vinícius Simões

Brazilian IT Support guy by day, and college student by night. IT enthusiast: like to follow up news, had some reading on IT issues but no degree.

Updated on September 18, 2022

Comments

  • Vinícius Simões
    Vinícius Simões over 1 year

    I work as IT Support for public service.

    Some of the jobs we have to do on a regular basis is to install some software developed by our own developers. We usually do it as procedures, in which in some cases we don't know exactly what we are doing, know just what to do. And in some cases the task in hand is to add some register keys in the Windows Register. But since many of these programs rely on basically the same databases, some of the steps were already applied. I am in the process of developing a batch file to check the steps which were and weren't taken.

    So I would like to know if I have one registry file, full of entries to edit in the Windows Registry, I can use the same entries to check if they were already applied to the registry. If I can copy the content of the registry key to a batch file to check the entries, or if I need to send both the batch file and the key file to make this procedure.

    • Vinícius Simões
      Vinícius Simões almost 9 years
      There are a significant amount of steps and the support is remote so it would be interesting to apply only the steps which weren't already taken.
    • Vinícius Simões
      Vinícius Simões almost 9 years
      I need conditional checking between the content and the keys which may exist or not in the registry. And we are talking about a significant amount of keys, in the house of tens.
    • Vinícius Simões
      Vinícius Simões almost 9 years
      I am not very keen on batch scripting, command line or Windows Registry as well... so if you please could be more assertive.
    • Vinícius Simões
      Vinícius Simões almost 9 years
      I mean to give more detailed answers.
  • Vinícius Simões
    Vinícius Simões almost 9 years
    If I put value, data and string as parameters to the command, it will return errorlevel 1 if they don't match exactly?
  • DavidPostill
    DavidPostill almost 9 years
    @ViníciusSimões I don't understand. Are you talking about query or add?
  • DavidPostill
    DavidPostill almost 9 years
    @ViníciusSimões query can only check for a value. data and string is only applicable to add.
  • DavidPostill
    DavidPostill almost 9 years
    @ViníciusSimões read ss64.com/nt/reg.html
  • Wolf
    Wolf over 7 years
    I still saw a message for non-existing keys on Win8.1, I suppressed it by appending 2>&1
  • w5m
    w5m over 5 years
    Is the 2nd "echo" in the correct place? Shouldn't it be on the next line immediately preceding "mykey exists- do nothing", rather than preceding the open bracket?
  • w5m
    w5m over 5 years
    I also had to add 2>&1 after >nul to suppress the message for non-existing keys on Win10.
  • DavidPostill
    DavidPostill over 5 years
    @w5m The echo will work in either position ...
  • w5m
    w5m over 5 years
    @DavidPostill It doesn't work for me as you currently have it. As the echo precedes the 1st open parenthesis it outputs that character and then produces an error '"mykey exists- do nothing"' is not recognized as an internal or external command, operable program or batch file." Moving the echo after the open parenthesis resolves the issue. I've edited the code accordingly.