How to Kill a Process by Name in Batch Script
One of the most common system management tasks is to terminate a running application or process. You might need to close a hung application, stop a background service before a file update, or ensure a clean state by closing all instances of a program. The standard, built-in command-line utility for this in Windows is the powerful taskkill.exe.
This guide will teach you how to use the taskkill command with its /IM switch to terminate a process based on its image (executable) name. You will learn how to perform both a graceful and a forceful termination and see how to use this command in a practical script to clean up running applications.
The Core Command: taskkill
The taskkill.exe utility is the primary tool for terminating one or more running processes from the command line. It can target processes in several ways, but the most common and powerful method for scripting is by the "image name," which is the name of the executable file (e.g., notepad.exe).
Targeting a Process by Image Name (/IM)
The /IM switch is used to identify the process(es) to terminate by their Image Mame. This is a very powerful feature because it will target all running instances that match the specified name.
Syntax: taskkill /IM "<ImageName>"
/IM: Specifies that you are targeting by Image Mame.<ImageName>: The name of the process's executable, such aschrome.exeornotepad.exe. It's a best practice to quote the name.
Basic Example: A Simple Process Kill
This script will terminate all running instances of the Windows Notepad application.
@ECHO OFF
TITLE Process Killer
ECHO --- Terminating all Notepad processes ---
ECHO.
ECHO Please open one or more Notepad windows to test this script.
PAUSE
ECHO Sending the termination command...
taskkill /IM "notepad.exe"
ECHO.
ECHO --- Command sent ---
What Happens
This command sends a standard "close" request to all processes named notepad.exe. If Notepad has unsaved work, it will prompt the user to save before closing. This is known as a graceful termination.
Forceful vs. Graceful Termination (/F)
Sometimes, an application is unresponsive or "hung" and will not respond to a graceful close request. In these cases, you need to force it to terminate.
- Graceful Termination (without
/F): Asks the application to close. The application can refuse or prompt the user. - Forceful Termination (with
/F): Forces the process to terminate immediately. The application is given no chance to save data or clean up. This is equivalent to "End Task" in the Task Manager.
Warning: Using /F can cause data loss in the target application.
Example Script for a Force Kill
@ECHO OFF
ECHO Forcefully terminating all Microsoft Word processes...
ECHO Any unsaved work will be lost.
PAUSE
taskkill /IM "winword.exe" /F
The /F switch is essential for unattended scripts that must ensure a process is stopped.
Common Pitfalls and How to Solve Them
Problem: "Access is denied." (Administrator Privileges)
If you try to kill a process that was started by another user, or a protected system process, your command will fail.
Example of error message:
ERROR: The process "SomeSystemProcess.exe" with PID 123 could not be terminated.
Reason: Access is denied.
Solution: For administrative scripts, you must run the script from an elevated command prompt. Right-click your .bat file or cmd.exe and select "Run as administrator."
Problem: The Process is Not Found
If no process with the specified image name is running, taskkill will report an error.
Example of error message:
ERROR: The process "MyMissingApp.exe" not found.
Solution: In a script, this error can be undesirable. The best practice is to first check if the process is running before you try to kill it. You can do this with tasklist.
@ECHO OFF
SET "ProcessName=MyMissingApp.exe"
tasklist /FI "IMAGENAME eq %ProcessName%" 2>NUL | find /I /N "%ProcessName%" > NUL
IF %ERRORLEVEL% EQU 0 (
ECHO Process is running. Terminating it now...
taskkill /IM "%ProcessName%" /F
) ELSE (
ECHO Process is not running. No action needed.
)
Practical Example: A "Close All Browsers" Script
This script is a simple utility to forcefully close all common web browsers. This can be useful for preparing a machine for a presentation or for ending a session.
@ECHO OFF
SETLOCAL
TITLE Browser Closer
ECHO --- Closing All Web Browsers ---
ECHO This will forcefully close Chrome, Firefox, and Edge.
ECHO Any unsaved tabs or data may be lost.
ECHO.
PAUSE
ECHO Closing Google Chrome...
taskkill /IM "chrome.exe" /F /T > NUL 2> NUL
ECHO Closing Mozilla Firefox...
taskkill /IM "firefox.exe" /F /T > NUL 2> NUL
ECHO Closing Microsoft Edge...
taskkill /IM "msedge.exe" /F /T > NUL 2> NUL
ECHO.
ECHO --- All browsers have been terminated ---
ENDLOCAL
The /T (Tree) switch is also used here. It terminates the main process and any child processes it started, which is a good practice for complex applications like browsers.