Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This relates to my thread Pass the PID from tasklist into taskkill to kill a process by the .dlls it's holding open.

I am trying to do something like what was discussed in that thread:

FOR /F "usebackq tokens=2 skip=2" %i IN (`tasklist |findstr /r "[0-9].exe") DO taskkill /PID %i

The above command does not run. I want to get all processes which are like 123456.exe and kill them. What am I doing wrong?

share|improve this question

You'll need to make a couple of changes to make it work. This example as you want:

FOR /F "usebackq tokens=1-2" %i IN (`tasklist ^|findstr /b "[0-9]"`) DO taskkill /PID %j

Your code:

FOR /F "usebackq tokens=2 skip=2" %i IN (`tasklist |findstr /r "[0-9].exe") DO taskkill /PID %i

Why your code was failing:

  • At runtime, the skip=2 directive will skip your first two results.
  • Tasklist.exe output has several columns. Findstr.exe acts on the first column, but Taskkill.exe uses the second. Without including both, Taskkill.exe has no pid to act on.
  • Your set (the string between the parentheses) must be delineated using back quotes (usebackq directive). Your set is missing the final back quote.
  • The |, or 'pipe' character is reserved. You must escape it with a ^ character.
  • The regular expression set you pass to Findstr.exe returned for me unintended results. I solved it by stripping away the .exe and instructing Findstr.exe to look for my string at the beginning of the piped stream. Limited in this way, my results didn't include programs like rundll32.exe which your set was including.

You can find some excellent script examples at Rob Vanderwoude's and Timo Salmi's sites:

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.