How to get Data from a registry key value via command line
19,479
This can be done very simply using a FOR
loop along-side it's Token
System. Since reg query
will output the variables in a one two three
format, we can use tokens=3
to grab only the third item in the output.
From CMD:
for /F "tokens=3" %A in ('reg query "HKCU\Software\[PATH_TO_MY_DIR]" /v "[KEY_NAME]"') DO (Echo %A)
From BATCH:
for /F "tokens=3" %%A in ('reg query "HKCU\Software\[PATH_TO_MY_DIR]" /v "[KEY_NAME]"') DO (Echo %%A)
Author by
Demodave
Been developing for 10 years. C#, PHP, HTML5, jQuery, CSS3 2 Bachelors Computer Science & e-Business. 1 Masters of Software Engineering.
Updated on June 04, 2022Comments
-
Demodave almost 2 years
I am trying to get the Data from a registry key value via command line
I can retrieve the value of a registry key using the following code
reg query HKCU\Software\[PATH_TO_MY_DIR] /v [KEY_NAME]
This works as expected and outputs three items:
- Name
- Type
- Data
I am trying to get the data from the value in command line how do I do this?
-
Stevoisiak over 4 yearsThis won't work if the registry value contains spaces.
-
Zunair about 4 yearschanging token to =3,* and adding %B in echo will take care of spaces for the last field. But you will have to change(add) the token if there is space in the keyname.
-
Uber Kluger over 3 yearsAlso,
reg query
outputs a blank line followed by a header listing the key path before the 'value/type/data' line. If this header contains 2 (or more) spaces (probably in the [PATH_TO_MY_DIR] or that with a subkey name) then it will produce spurious output. So should befor /f "skip=2 tokens=...
(note: even though a blank line is ignored when tokenizing, it must be included in the skip count) -
Hasan Merkit about 3 yearsIt's worked for me with spaces, thanks Zunair. : for /F "tokens=3*" %A in ('reg query "HKCU\SOFTWARE\Valve\Steam" /v "SteamExe"') DO (Echo %A %B)
-
NiKiZe almost 3 yearsusing
FOR /F "tokens=2*" %%A IN ('reg query ...') DO echo %%~B
If registry value is a string then%%A
will haveREG_SZ
and the remainder will be pure in%%B