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

Demodave picture Demodave · Dec 28, 2018 · Viewed 11.2k times · Source

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?

Answer

John Kens picture John Kens · Dec 29, 2018

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)