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
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: