Search code examples
batch-filefor-loopsyntaxwmic

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


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
)

Solution

  • @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.