Batch Loop used to reg query a list of keys from txt file and reuse data value

1

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.

Example:

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

COMMAND:

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

RESULT:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\S-1-5-18\Products\01690987922DC9549A63529D22383DDF\InstallProperties
    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.

Heinza

Posted 2016-03-23T13:52:44.597

Reputation: 31

2Do you need to use the cmd language or can you upgrade to a more capable tool (powershell, python, perl)? – Alien Life Form – 2016-03-23T14:00:14.450

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 – 2016-03-23T14:46:06.060

@AlienLifeForm cmd is perfectly capable of such a trivial task ;) – DavidPostill – 2016-03-23T18:54:44.553

@AlienLifeForm I also have cygwin as well. I just haven't gotten around to learning powershell. – DavidPostill – 2016-03-24T08:25:02.533

Answers

1

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
    )
  )

Notes:

  • 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

DavidPostill

Posted 2016-03-23T13:52:44.597

Reputation: 118 938