Batch File To Search and Destroy a Process

Tech Talk : Batch File – Look for Process And Kill ItThe batch script below outlines how to search and kill a process; for example vlc.exe process running on a system.

:search
tasklist|find "vlc"
IF %ERRORLEVEL% = 0 THEN (GOTO found)
TIMEOUT /T 1
GOTO search

:found
taskkill /im vlc.exe

The batch script pipes the output from tasklist into find command which searchs for “vlc*”, and use an IF-statement and a GOTO to continue looping, checking for that process, until it is found. Once it is found, the script executes the “GOTO found” statement which allows it to jump to “:found” to kill the task.

Note that the TIMEOUT statement pauses the script for 1 second. This will help prevent the script from utilizing too much CPU constantly running that loop.

Side note for people who have never written batch scripting before, the steps below outlines how to create a batch script file.

  1. Open Notepad (Start->All Programs->Accessories->Notepad).
  2. Copy the script above and paste it into the notepad.
  3. In the Notepad, save the script as “kill.bat”; “File->Save As”, and enter file name as “kill.bat” (with double quotes) and click on “Save” button.

Note the directory location of where you have saved the batch file.