How to List All Running Processes in Batch Script
Viewing the list of currently running processes is one of the most fundamental diagnostic tasks on a Windows system. It allows you to see which applications and background services are active, how much memory they are using, and their unique Process ID (PID). The standard, built-in command-line utility for this task is tasklist.exe.
This guide will teach you how to use the tasklist command to display all running processes, how to format the output for easy scripting, and how to use its powerful filtering capabilities to find specific processes you're interested in.
The Core Command: tasklist
The tasklist.exe utility is the command-line equivalent of the "Processes" or "Details" tab in the Windows Task Manager. It provides a snapshot of all tasks running on the local (or a remote) computer.
The basic syntax is: tasklist [switches]
Basic Example: A Simple Process List
Running tasklist with no arguments gives a simple, table-formatted list of all processes visible in your current user session.
@ECHO OFF
ECHO --- Listing All Currently Running Processes ---
ECHO.
tasklist
The default output is a table with several key columns.
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
System Idle Process 0 Services 0 8 K
System 4 Services 0 1,180 K
svchost.exe 848 Services 0 11,540 K
notepad.exe 5432 Console 1 25,484 K
chrome.exe 6789 Console 1 150,321 K
... (and many more) ...
- Image Name: The name of the executable file (e.g.,
notepad.exe). - PID: The Process ID. A unique number assigned to this specific instance of the program. This is crucial for commands like
taskkill. - Session Name: Indicates if the process is a background
Serviceor an interactiveConsoleapplication. - Mem Usage: The amount of memory the process is currently using.
Key tasklist Parameters (/FO, /V, /FI)
To make the output more useful for scripting, you can use several command-line switches.
/FO <FORMAT>: Format Output. This is the most important switch for scripting.TABLE: The default, human-readable table.LIST: A detailed list of properties for each process.CSV: Comma-Separated Values. This is the best format for scripting.
/V: Verbose. Provides more detailed information, including the process status (Running/Not Responding) and the window title./FI "<Filter>": Filter. Allows you to select processes based on specific criteria./NH: No Header. Suppresses the header line, which can simplify parsing.
How to Capture and Parse the Process List in a Script
To use the process list in a script, you should format the output as CSV and then parse it with a FOR /F loop. This allows you to process each task's properties individually.
@ECHO OFF
SETLOCAL
ECHO --- Capturing Process Information (PID and Name) ---
ECHO.
REM 'skip=1' ignores the CSV header. 'tokens=1,2' grabs the first two columns.
FOR /F "skip=1 tokens=1,2 delims=," %%A IN ('tasklist /FO CSV') DO (
ECHO Process Name: %%~A, PID: %%~B
)
ENDLOCAL
%%~A and %%~B are used to remove the quotes that the CSV format adds around each field.
Filtering the List (/FI) to Find Specific Processes
The /FI switch is the most powerful feature of tasklist for scripting. It allows you to find processes without having to parse the entire list.
Example: Script to Find a Specific Process
This script checks if notepad.exe is running.
@ECHO OFF
ECHO --- Searching for Notepad ---
tasklist /FI "IMAGENAME eq notepad.exe"
Common Filters
IMAGENAME eq notepad.exe: Find a process by its exact executable name.PID eq 5432: Find a process by its exact Process ID.STATUS eq Not Responding: Find all processes that are hung or unresponsive.USERNAME eq "DOMAIN\user": Find all processes running under a specific user account.
Common Pitfalls and How to Solve Them
Problem: The Default Output is Hard to Parse
The default TABLE format is difficult to parse reliably because the columns are separated by a variable number of spaces.
Solution: Always use the /FO CSV switch when you intend for your script to process the output of tasklist. The comma is a reliable delimiter that makes parsing with FOR /F simple and robust.
Problem: Not All Processes are Visible (Administrator Rights)
When you run tasklist as a standard user, you can only see the processes running under your own account. You will not see system services or processes running under other user accounts.
Solution: For a complete and accurate view of all processes running on the system, you must run the script as an Administrator.
Practical Example: A Script to Find Unresponsive Tasks
This script uses the verbose (/V) output and a filter (/FI) to find all programs that are currently hung and reports their name and PID.
@ECHO OFF
SETLOCAL
ECHO --- Searching for "Not Responding" Applications ---
ECHO.
REM We use the /V switch to get the STATUS column.
REM We use the /FI switch to filter for only hung tasks.
REM We use /FO CSV to make it easy to parse.
FOR /F "skip=1 tokens=1,2 delims=," %%A IN (
'tasklist /V /FI "STATUS eq Not Responding" /FO CSV'
) DO (
ECHO [HUNG] Process: %%~A, PID: %%~B
)
ENDLOCAL
Conclusion
The tasklist command is the standard, built-in tool for listing running processes from the command line. It is an essential utility for any diagnostic, monitoring, or process management script.
Key takeaways for using it in scripts:
- For scripting, always use the
/FO CSVswitch to produce clean, parsable output. - Use the powerful
/FIswitch to filter the list and find specific processes without manual parsing. - Run your script as an Administrator to get a complete list of all system and user processes.
- Use a
FOR /Floop to process the CSV output and use the data (like the PID) in your script.