Checking if a key is present on the windows registry through batch file
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.
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
-
reg - Read, Set or Delete registry keys and values, save and restore from a .REG file.
-
An A-Z Index of the Windows CMD command line is an excellent reference for all things Windows cmd line related.