Search code examples
batch-filewindows-server-2008-r2tasklist

Scripting process names with spaces when using TASKLIST command in a bat file


I'm trying to run a bat file that has a TASKLIST command for a process with a space in the name. I'm using the following code:

set "exe=The Process.exe"
tasklist /FI "PROCESSES eq %exe%"
...

but it returns

C:\folder>set "exe=The Process.exe"
C:\folder>tasklist /FI "PROCESSES eq The Process.exe"
ERROR: The search filter cannot be recognized.

So how do I go about running a tasklist command in a bat file for a process with a space in the name?


Solution

  • I believe you have used an invalid keyword in your tasklist filter - PROCESSES should be IMAGENAME.

    set "exe=The Process.exe"
    tasklist /FI "IMAGENAME eq %exe%"
    ...
    

    For more filters, take a look at the help for the tasklist command - tasklist /?