- 1 year ago
Get only PID from tasklist using cmd title
In Windows Command Prompt (cmd), you can use the tasklist
command along with the findstr
command to filter the output and get only the Process ID (PID) of a specific task with a given window title.
Here's the command to achieve this:
batch@echo off
set "WindowTitle=YourWindowTitle"
for /f "tokens=2" %%i in ('tasklist /v ^| findstr /i /c:"%WindowTitle%"') do (
set "PID=%%i"
)
echo The PID of the task with title "%WindowTitle%" is: %PID%
Replace YourWindowTitle
with the title of the task for which you want to find the PID. The script will use tasklist /v
to get a detailed list of tasks, and then it will use findstr
to filter the output and find the line containing the specified window title. Finally, it will use the for /f
loop to extract the PID from that line and store it in the PID
variable.
Note that the window title is not a unique identifier, and there could be multiple tasks with the same title. The script will return the PID of the first matching task it finds. If there are multiple tasks with the same title, you may need to implement additional logic to handle this situation, depending on your specific use case.