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

9

2

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

Posted 2015-07-04T21:10:39.140

Reputation: 155

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 – 2015-07-04T21:31:44.947

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 – 2015-07-04T21:39:43.820

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 – 2015-07-04T21:43:58.040

I mean to give more detailed answers. – Vinícius Simões – 2015-07-04T21:48:33.330

Answers

15

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

DavidPostill

Posted 2015-07-04T21:10:39.140

Reputation: 118 938

If I put value, data and string as parameters to the command, it will return errorlevel 1 if they don't match exactly? – Vinícius Simões – 2015-07-04T22:37:17.003

@ViníciusSimões I don't understand. Are you talking about query or add? – DavidPostill – 2015-07-04T22:41:42.253

@ViníciusSimões query can only check for a value. data and string is only applicable to add. – DavidPostill – 2015-07-04T22:44:17.597

@ViníciusSimões read http://ss64.com/nt/reg.html

– DavidPostill – 2015-07-04T22:45:05.210

1I still saw a message for non-existing keys on Win8.1, I suppressed it by appending 2>&1 – Wolf – 2017-02-02T16:29:22.467

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 – 2018-10-12T15:48:39.983

1I also had to add 2>&1 after >nul to suppress the message for non-existing keys on Win10. – w5m – 2018-10-12T16:34:52.960

@w5m The echo will work in either position ... – DavidPostill – 2018-10-12T19:22:07.550

@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. – w5m – 2018-10-17T15:13:47.700