I need a batch loop that should extract a data value and use it in its next command. All registry keys I want to query has been saved into a *.txt file.


The following command will return the key value, type and data as per below.


reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\01690987922DC9549A63529D22383DDF\InstallProperties /V UninstallString


    UninstallString    REG_EXPAND_SZ    MsiExec.exe /X{78909610-D229-459C-A936-25D92283D3FD}

What I want to do is to extract only the data value MsiExec.exe /X{78909610-D229-459C-A936-25D92283D3FD} and run it as my next command in the for loop.

I have roughly 20 registry keys that I want to query and then run the extracted command. I also want to add silent switches to the msiexec.exe command when it is executed.

I'm very new to batch loops and believe I need to use tokens/delims(?)... not really sure how to get it working.

  • 2
    Do you need to use the cmd language or can you upgrade to a more capable tool (powershell, python, perl)? – Alien Life Form Mar 23 '16 at 14:00
  • Other methods are OK. We use Windows ADK/MDT for unattended OS/application installations and any form of legacy package should work fine. I reckon powershell would be the best fit if I take my colleagues into consideration. – Heinza Mar 23 '16 at 14:46
  • @AlienLifeForm cmd is perfectly capable of such a trivial task ;) – DavidPostill Mar 23 '16 at 18:54
  • @AlienLifeForm I also have cygwin as well. I just haven't gotten around to learning powershell. – DavidPostill Mar 24 '16 at 8:25

reg query a list of keys from a txt file and reuse data values

reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\01690987922DC9549A63529D22383DDF\InstallProperties /V UninstallString

What I want to do is to extract only the data values MsiExec.exe and /X{78909610-D229-459C-A936-25D92283D3FD} and run it as my next command in the for loop.

The following batch file should get you started:

@echo off
setlocal enableDelayedExpansion
rem get each key from keys.txt
for /f "usebackq tokens=*" %%i in (`type keys.txt`) do (
  echo Processing key %%i
  rem skip the first line and grab tokens 3 and 4 from the second line
  for /f "usebackq skip=1 tokens=3,4" %%j in (`reg query %%i`) do (
    echo %%j /quiet %%k


  • keys.txt should contain the keys to query, one per line.
  • You need two for loops, the first to process each key, the second (inner) to parse the output.
  • We skip the first line of output as it is the name of the key.
  • Remove the echo from echo %%j /quiet %%k when you are happy with what the batch file is doing.
  • I assume /quiet is the switch you need for "silent". Change as necessary.

Further Reading

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.