Windows – WMIC in for-loop

batchwindowswmic

I'm trying to get WMIC output into a variable so I can process it further.

I have made a test batch file to illustrate the problem:

wmic PROCESS where "commandline like '%%teststr%%'" get     Processid,Caption,Commandline
for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid,Caption,Commandline`) do echo OUTPUT is %%R

Having called this batch I get the expected output for the first line, but invalid GET expression for the second.

Since the first line does work I think there is something wrong with my quoting – could someone please shed a light on this? I triple-checked it syntactically and it all seems correct to me according to this other question: Wmic output into variable

Edit1: %teststr% is just a string to filter, it could be javaw for example to look for certain java instances.

Edit2: Exact output is:

Caption    CommandLine                                                                                                                  ProcessId
javaw.exe  "C:\Program Files (x86)\Java\jre1.8.0_91\bin\javaw.exe" -jar "J:\tools\sonst\jEdit\jedit.jar" -reuseview -background -nogui  5152
javaw.exe  "C:\Program Files (x86)\Java\jre1.8.0_91\bin\javaw.exe" -jar "J:\tools\sonst\jEdit\jedit.jar" -reuseview -background -nogui  11504
javaw.exe  "c:\Program Files (x86)\Java\jdk1.7.0_80\bin\javaw.exe"  -jar "j:\tools\online\JBinUp\JBinUp.jar"                            16336
WMIC.exe   wmic  PROCESS where "commandline like '%javaw%'" get Processid,Caption,Commandline                                           18740

Invalid GET Expression.

BB

Best Answer

I get invalid GET expression for the second command.

for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid,Caption,Commandline`) do echo OUTPUT is %%R

You need to escape the , (commas) in the for expression, using the ^ Escape character:

for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid^,Caption^,Commandline`) do echo OUTPUT is %%R

Notes:

  • You might also want to add skip=1 to the for command to skip the header.
  • You will get an extra blank line at the end of the wmic output.
  • Use findstr to strip the blank lines from wmic output, as follows:
for /F "usebackq" %%R in (`wmic PROCESS where "commandline like '%%teststr%%'" get Processid^,Caption^,Commandline ^| findstr /r /v "^$"`) do echo OUTPUT is %%R

Test batch file:

@echo off
setlocal EnableDelayedExpansion
wmic process where "Commandline like '%%note%%'" get Processid,Caption,Commandline
for /f "usebackq" %%r in (`wmic process where "commandline like '%%note%%'" get Processid^,Caption^,Commandline ^| findstr /r /v "^$"`) do echo OUTPUT is %%r
endlocal

Example output:

F:\test>test
Caption                   CommandLine                                                                                                                            ProcessId
GSNotes.exe               "E:\GoldenSectionNotes\GSNotes.exe"                                                                                                    8864
LiberKeyPortabilizer.exe  "E:\LiberKey\LiberKeyTools\LiberKeyPortabilizer\LiberKeyPortabilizer.exe" /app="E:\LiberKey\Apps\Notepad++\Notepad++LKL.dat"  /lkpend  12324
notepad++.exe             "E:\LiberKey\Apps\Notepad++\App\Notepad++\notepad++.exe"                                                                               11948
WMIC.exe                  wmic  process where "Commandline like '%note%'" get Processid,Caption,Commandline                                                      1364

OUTPUT is Caption
OUTPUT is GSNotes.exe
OUTPUT is LiberKeyPortabilizer.exe
OUTPUT is notepad++.exe
OUTPUT is cmd.exe
OUTPUT is WMIC.exe

Further Reading

Related Question