Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only PID from tasklist using cmd title

Desired output:

1234

Just the PID. Nothing else - no other characters, numbers, or symbols.

I'm trying to run tasklist so it gives me only the PID of a named or titled process.

tasklist | findstr /i "cmd.exe" is the closest I've gotten, but the result is too verbose. I just want the PID number.

Bonus points for linking me a description of what the tasklist filter operators mean - "eq", "ne", etc, since they aren't anywhere in the documentation.

like image 382
GregariousJB Avatar asked May 27 '18 19:05

GregariousJB


People also ask

How do I find my PID in CMD?

Use the Command PromptIn the Start menu search bar, search for command prompt and select Run as administrator. Type tasklist. Press Enter. Command Prompt will now display the PID for the running processes.

What does tasklist do in CMD?

Displays a list of currently running processes on the local computer or on a remote computer. Tasklist replaces the tlist tool. This command replaces the tlist tool.

How do I find the PID of a process in Windows?

Task Manager can be opened in a number of ways, but the simplest is to select Ctrl+Alt+Delete, and then select Task Manager. In Windows, first click More details to expand the information displayed. From the Processes tab, select Details to see the process ID listed in the PID column.

How do I see processes in CMD?

To view the list of the processes that are currently running, you can use the tasklist command, both in Command Prompt and PowerShell. Type tasklist and press Enter.


1 Answers

The difficult thing with tasklist is its default output format. For example, when command line:

tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running"

is executed, we get:

Image Name                     PID Session Name        Session#    Mem Usage
========================= ======== ================ =========== ============
cmd.exe                      12740 Console                    1      3'328 K
cmd.exe                      11020 Console                    1      3'304 K

Unless the column widths are fixed, which I would not rely on, extracting the PID is not that trivial, because the image name could also have SPACEs in it, so using such as delimiters would not work.
A possible way was to count the number of =-signs in the second line up to the first SPACE, so we know the number of characters to truncate to have the image name removed, but this requires some kind of loop (using goto), so the performance might be quite bad.

However, there are other output formats available for tasklist. The command line:

tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO CSV

results in this output:

"Image Name","PID","Session Name","Session#","Mem Usage"
"cmd.exe","12740","Console","1","3'328 K"
"cmd.exe","11020","Console","1","3'304 K"

Now it is quite easy to extract the PID:

@echo off
for /F "delims=" %%R in ('
    tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO CSV /NH
') do (
    set "FLAG1=" & set "FLAG2="
    for %%C in (%%R) do (
        if defined FLAG1 (
            if not defined FLAG2 (
                echo %%~C
            )
            set "FLAG2=#"
        )
        set "FLAG1=#"
    )
)

Another output formats is used by the following command line:

tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO LIST

resulting in this output:

Image Name:   cmd.exe
PID:          12740
Session Name: Console
Session#:     1
Mem Usage:    3'328 K

Image Name:   cmd.exe
PID:          11020
Session Name: Console
Session#:     1
Mem Usage:    3'304 K

With this it is even simpler to get the desired output:

@echo off
for /F "tokens=2" %%K in ('
   tasklist /FI "ImageName eq cmd.exe" /FI "Status eq Running" /FO LIST ^| findstr /B "PID:"
') do (
   echo %%K
)

By the way, for the filter options /FI, there are the following operators available:

  • eq -- equal to;
  • ne -- not equal to;
  • gt -- greater than;
  • lt -- less than;
  • ge -- greater than or equal to;
  • le -- less than or equal to;

The Microsoft documentation as well as the help message (tasklist /?) do not explain their meaning, but I found the following external resources:

  • Managing Windows Programs from the Command Line: Tasklist
  • Microsoft DOS tasklist command
like image 168
aschipfl Avatar answered Sep 27 '22 23:09

aschipfl