How to get Data from a registry key value via command line
Asked Answered
M

1

10

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?

Moorish answered 28/12, 2018 at 16:13 Comment(0)
G
6

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)
Gorlicki answered 29/12, 2018 at 1:30 Comment(5)
This won't work if the registry value contains spaces.Lissy
changing 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.Whitehot
Also, 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 be for /f "skip=2 tokens=... (note: even though a blank line is ignored when tokenizing, it must be included in the skip count)Balduin
It'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)Esta
using FOR /F "tokens=2*" %%A IN ('reg query ...') DO echo %%~B If registry value is a string then %%A will have REG_SZ and the remainder will be pure in %%BDeoxidize

© 2022 - 2024 — McMap. All rights reserved.