Skip to main content

How to Check if a Process is Running in a Batch Script

A common requirement in automation is for a script to know whether a specific program is already running. You might need to check if a critical service is active, prevent a script from launching a second instance of an application that should only run once, or wait for a process to start before you interact with it.

This guide will teach you the standard and most reliable method for checking if a process is running by using the built-in TASKLIST command combined with the FINDSTR filter. You will learn how to use the exit code (%ERRORLEVEL%) from this command to make decisions in your script, and how to create a "singleton" script that prevents itself from running more than once.

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 displays a list of all currently running processes on the local (or a remote) computer.

Command:TASKLIST

This command produces a table of all running processes, including their image name, Process ID (PID), session name, and memory usage.

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
...
svchost.exe 1024 Services 0 15,820 K
notepad.exe 8080 Console 1 25,432 K
chrome.exe 9120 Console 1 150,112 K
...

Our goal is to search this list for our target process.

The Method: Filtering TASKLIST with FINDSTR

To check for a specific process, we can pipe the output of TASKLIST into the FINDSTR command (or the simpler FIND). FINDSTR will search for a specific string (our process name) and set its exit code based on whether it found a match.

Command: TASKLIST | FINDSTR /I "ProcessName.exe"

  • |: The pipe sends the output of TASKLIST as input to FINDSTR.
  • /I: This switch makes the search case-Insensitive, which is a good practice.
  • "ProcessName.exe": The name of the executable you are looking for.

The Script: A Basic "Is Running?" Check

This script checks if notepad.exe is currently running.

@ECHO OFF
SET "ProcessName=notepad.exe"

ECHO --- Checking if %ProcessName% is running ---
ECHO.

TASKLIST | FINDSTR /I "%ProcessName%" > NUL

IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] The process "%ProcessName%" is running.
) ELSE (
ECHO [INFO] The process "%ProcessName%" is not currently running.
)

How the Script Works by Checking ERRORLEVEL

The FINDSTR command is the key to this script's logic. It sets the %ERRORLEVEL% variable based on the outcome of its search:

  • ERRORLEVEL 0: Success. The search string (e.g., "notepad.exe") was found in the task list. This means the process is running.
  • ERRORLEVEL 1: Failure. The search string was not found. This means the process is not running.

The line > NUL is used to suppress the output of the FINDSTR command. We don't need to see the line it found; we only care about the success or failure signal provided by %ERRORLEVEL%.

Key TASKLIST Parameters Explained

  • /S <computer>: Specifies a remote System to connect to. You can check for processes on another machine. Requires administrative rights on the remote machine.
  • /U <user> /P <password>: Specifies the User and Password credentials to use for a remote machine.
  • /FI "<filter>": Provides more powerful, server-side Filtering. This is more efficient than piping to FINDSTR. (TASKLIST /FI "IMAGENAME eq notepad.exe")
note

The /FI switch is the more "professional" way to do this, as the filtering is done by the TASKLIST command itself.

Example of Script using the /FI filter

REM The /NH (No Header) switch is used to prevent the header from matching our search.
TASKLIST /FI "IMAGENAME eq notepad.exe" /NH | FINDSTR /I "notepad.exe" > NUL

Common Pitfalls and How to Solve Them

  • Partial Matches: A command like TASKLIST | FINDSTR "app.exe" will also match a process named my_bad_app.exe.

    • Solution: Make your filter more specific. The TASKLIST /FI "IMAGENAME eq ..." method is perfect for this, as it checks for an exact match. Alternatively, with FINDSTR, you can anchor the search to the beginning of the line: FINDSTR /B /I "app.exe".
  • Permissions: A standard user can only see their own processes. They cannot see processes running under other user accounts or as the SYSTEM account.

    • Solution: For a complete view of everything running on the system, you must run the script as an Administrator.

Practical Example: A "Singleton" Script (Preventing Duplicate Runs)

This is a classic and very useful script. It checks if another instance of itself is already running. If it is, the new script exits immediately. This is done by giving the script's window a unique title and then checking for a process with that window title.

@ECHO OFF
SETLOCAL

REM --- Give this script's window a unique, identifiable title ---
SET "MyTitle=MyUniqueApp_Singleton_Mutex"
TITLE %MyTitle%

ECHO --- Singleton Script ---
ECHO Checking for other running instances...

REM Use TASKLIST with a window title filter.
REM /V gives verbose output which includes the window title.
REM We must filter out our OWN process, which we can identify by its PID.
tasklist /v /fi "WINDOWTITLE eq %MyTitle%" | findstr /v /c:"%PID%" > nul

REM FINDSTR sets errorlevel 0 if it finds another process.
IF %ERRORLEVEL% EQU 0 (
ECHO [ERROR] Another instance of this script is already running.
ECHO Exiting now.
TIMEOUT /T 5
GOTO :EOF
)

ECHO [SUCCESS] No other instance found. This is the only instance.
ECHO.
ECHO Starting main work...
PAUSE

ENDLOCAL
note

This advanced example relies on newer Windows features like %PID%.

Conclusion

The TASKLIST command is the definitive tool for checking for running processes from a batch script.

For reliable process checking:

  • The standard method is to pipe the output of TASKLIST into FINDSTR and check the %ERRORLEVEL%.
  • Use the more efficient TASKLIST /FI "IMAGENAME eq ..." filter for a more robust and precise check.
  • Run your script as an Administrator to get a complete list of all processes running on the system.

By using TASKLIST, you can create intelligent scripts that are aware of their environment and can avoid common errors like starting a duplicate process.