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

Windows scripting noob here. Say I've got the name of a .dll I need to delete or copy over, and I'm getting the classic "The file is in use by another process..." error. I know I can run tasklist with the /m switch and pass it the dll name. Then I can get the PID for the processes and kill them with taskkill.

How can I do this in one step in a batch file?

share|improve this question
up vote 2 down vote accepted

Reminder: in a batch file you will need to use the double "%%" instead a single

FOR /F "usebackq tokens=2 skip=2" %i IN (`tasklist /m winsta.dll`) DO taskkill /PID %i
share|improve this answer
this is awesome, you are awesome – Chris McCall Dec 15 '09 at 17:18
Well thank you! – Goyuix Dec 15 '09 at 20:48
be aware that "skip=2" means to skip the first two lines of output, in case you want to tweak this (ref:…) – rogerdpack Jun 24 '15 at 9:25

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

will automatically kill the tomcat process.

share|improve this answer


I can't seem to get the tasklist results to parse. FOR /F "usebackq tokens=2 skip=2" %i IN ('tasklist /m winsta.dll') DO echo %i The tasklist command by itself is fine, but the loop is not recognizing the tasklist resuls. Rather it appears to be parsing the string. I can output the task list results to file and parse with the loop, but I thought I might be missing something simple.

share|improve this answer
use a backtick (same key as the tilde on most U.S. layout keyboards) - not the single quote. Try that. – Goyuix Dec 15 '09 at 21:21

yep. simple. backquote, not single quote.

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.