Search code examples
windowscmdtasklist

Build cmd command to extract PID from tasklist and use condition on the result


I'm trying to write one line cmd command that:

  • execute tasklist with PID filter
  • iterate over the output
    • do something if PID found
    • do something else if PID was not found

So I've read How to extract a specific field from output of tasklist on the windows command line and used this answer to construct this command:

for /f "tokens=2 delims=," %F in ('tasklist /nh /fi "PID eq 5284" /fo csv') do if %F NEQ "5284" (@echo no) else  (@echo yes)

I didn't get the expected result. Most likely got the syntax wrong.


Solution

  • Just posting the answer commented by eryksun:

    (for /f "tokens=2 delims=," %F in ('tasklist /nh /fo csv /fi "PID eq 5284"') do @echo yes) || @echo no