Search code examples
pidtaskkill

What is PID advantage?


For example, to kill all java processes in background, I found two ways to do it by batch script. One is using PID, and the other one is not. What's the differences between these two methods, and what's the advantage using PID?

Without PID:

taskkill /F /IM java.exe

With PID:

FOR /F "usebackq tokens=2 skip=2" %%i IN (`TASKLIST /FI "IMAGENAME eq java.exe"`) DO taskkill /F /PID %%i

Solution

  • They are equivalent so you should just use the first version. taskkill with /IM matches the image name. In the second case, you are manually building a list of PIDs matching the same image name, and then killing them one-by-one in a loop. You would use the PID version when you need more control over the specific processes being killed instead of just everything with a particular image name.