Skip to main content

How to Find a Process by Window Title in Batch Script

In scripting, especially for automation and system management, you often need to interact with a running application based on its window title. You might want to check if a specific program is already open, or you might need to find the Process ID (PID) of a stuck application so you can terminate it. While you can't get the window title from wmic process, the versatile tasklist command has a built-in filter specifically for this purpose.

This guide will teach you how to use the tasklist command with its window title filter to find a process. You will learn how to capture the Process ID (PID) into a variable and how to use wildcards for partial title matches, enabling you to create powerful scripts that can manage GUI applications.

The Core Command: tasklist /FI "WINDOWTITLE ..."

The tasklist.exe utility is the standard command-line tool for listing all running processes. Its /FI (Filter) switch is the key to our solution, allowing us to filter the list based on various criteria, including the window title.

Syntax: tasklist /FI "WINDOWTITLE eq <WindowTitle>"

  • /FI: Specifies that we are applying a Filter.
  • "WINDOWTITLE eq <WindowTitle>": The filter string.
    • WINDOWTITLE: The property we are checking.
    • eq: The operator, meaning "equal to".
    • <WindowTitle>: The exact, case-sensitive title of the window you are looking for.

Basic Example: Finding a Specific Window

Let's find the process for an open Notepad window that has not yet been saved. Its default title is "Untitled - Notepad".

@ECHO OFF
ECHO --- Searching for 'Untitled - Notepad' ---
ECHO.
tasklist /FI "WINDOWTITLE eq Untitled - Notepad"

If the window is found, tasklist will display a standard process list as output, but only for the matching process(es).

--- Searching for 'Untitled - Notepad' ---

Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
notepad.exe 5432 Console 1 25,484 K

How to Capture the Process ID (PID) in a Script

For scripting, just seeing the process isn't enough. You need to capture its PID so you can take action, like terminating it. A FOR /F loop is the perfect tool for parsing the output of tasklist.

@ECHO OFF
SET "WindowName=Untitled - Notepad"
SET "ProcessPID="

ECHO --- Capturing PID for window: "%WindowName%" ---

REM 'skip=3' ignores the header lines. 'tokens=2' grabs the second column (the PID).
FOR /F "skip=3 tokens=2" %%P IN (
'tasklist /FI "WINDOWTITLE eq %WindowName%"'
) DO (
SET "ProcessPID=%%P"
GOTO :PidCaptured
)

:PidCaptured
IF NOT DEFINED ProcessPID (
ECHO [FAILURE] A process with that window title was not found.
) ELSE (
ECHO [SUCCESS] The Process ID is: %ProcessPID%
)
  • skip=3: Skips the Image Name..., =======..., and blank line headers.
  • tokens=2: Selects the second piece of the line, which is the PID.
  • GOTO :PidCaptured: Exits the loop after the first match is found.

Using Wildcards (*) for Partial Matches

What if you want to find a Notepad window for a specific file, but you don't know the full title (e.g., MyDocument.txt - Notepad)? For this, you need a partial match. You must change the operator from eq (equals) to lk (like) and use a wildcard (*).

Syntax: tasklist /FI "WINDOWTITLE lk <PartialTitle>*"

  • lk: The like operator, which enables wildcard matching.
  • *: The wildcard character, which matches any sequence of characters.

This script will find any Notepad window, regardless of the file it has open.

@ECHO OFF
ECHO --- Finding any Notepad window ---
tasklist /FI "WINDOWTITLE lk *Notepad"

Common Pitfalls and How to Solve Them

Problem: The Window Title is Not Found

If no window matches your filter, tasklist will print a message and set the %ERRORLEVEL% to a non-zero value.

INFO: No tasks are running which match the specified criteria.

Solution: A robust script should check the %ERRORLEVEL% after the tasklist command.

tasklist /FI "WINDOWTITLE eq MyMissingWindow" > NUL 2> NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO The window was not found.
)

Problem: The Window Title Contains Quotes

This is a very tricky edge case. If a window title itself contains double quotes, it can break the tasklist filter syntax.

Solution: There is no easy "pure-batch" solution for this. It requires advanced escaping or, preferably, using a more powerful tool like PowerShell, whose parsers are designed to handle these cases. For the vast majority of window titles, this is not an issue.

Practical Example: A "Kill by Window Title" Script

This is the most common and practical use case. The script takes a partial window title as an argument, finds the corresponding PID, and then uses taskkill to terminate the process.

@ECHO OFF
SETLOCAL
SET "WindowTitle=%~1"

IF "%WindowTitle%"=="" (
ECHO [ERROR] Please provide a partial window title.
ECHO Usage: %~n0 "Untitled - Notepad"
GOTO :End
)

ECHO --- Attempting to terminate process with window title like "*%WindowTitle%*" ---
SET "ProcessPID="

FOR /F "skip=3 tokens=2" %%P IN (
'tasklist /NH /FI "WINDOWTITLE lk *%WindowTitle%*"'
) DO (
SET "ProcessPID=%%P"
GOTO :PidFound
)

:PidFound
IF NOT DEFINED ProcessPID (
ECHO [INFO] No matching process found.
GOTO :End
)

ECHO Found matching process with PID: %ProcessPID%.
ECHO Terminating process...
taskkill /PID %ProcessPID% /F

:End
ENDLOCAL
note

/NH in tasklist means No Header, which simplifies the FOR /F loop, though skip is still a safe practice.

Conclusion

The tasklist command is the definitive tool for finding processes based on their window titles, a crucial capability for managing GUI applications from a script.

Key takeaways for success:

  • Use tasklist /FI "WINDOWTITLE eq <ExactTitle>" for an exact, case-sensitive match.
  • Use tasklist /FI "WINDOWTITLE lk <PartialTitle>*" for a case-sensitive wildcard match.
  • Use a FOR /F "skip=3 tokens=2" loop to reliably capture the Process ID (PID) into a variable.
  • You can then use the captured PID with other commands, most commonly taskkill /PID %ProcessPID% /F.