Windows – Pass the PID from tasklist into taskkill to kill a process by the .dlls it’s holding open

batch-filewindows

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?

Best Answer

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