Combine For /F with WMIC + WHERE clause + AND clause

ElektroStudios picture ElektroStudios · Oct 21, 2013 · Viewed 9.9k times · Source

How should this WMIC command be written when included in a FOR command in a script?

wmic service where (name="themes" and state="running") get

The code below does not work:

For /F %%a in (
    'wmic service where ^("name='themes'" and "state='running'"^) get'
) do (
    echo %%a
)

Answer

npocmaka picture npocmaka · Oct 21, 2013
@echo off
For /F "usebackq delims=" %%a in (`wmic service where 'name^="themes" and state^="running"' get`) do (
    echo %%a
)

this one works for me.I've used usebackq option to have no problems with ' and alternative wmic syntax - ' instead of brackets.