Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Process Name by its Process ID [closed]

Suppose I know the process ID. I want to find the process name by its ID, using windows batch script. How can I do this?

like image 974
Oz Molaim Avatar asked Nov 30 '14 08:11

Oz Molaim


People also ask

What is the command to display the name of a process?

You can use the ps command to find out which processes are running and display information about those processes. The ps command has several flags that enable you to specify which processes to list and what information to display about each process.

What is the name of the process that runs with the process ID 1?

Process ID 1 is usually the init process primarily responsible for starting and shutting down the system.


2 Answers

The basic one, ask tasklist to filter its output and only show the indicated process id information

tasklist /fi "pid eq 4444"  

To only get the process name, the line must be splitted

for /f "delims=," %%a in ('     tasklist /fi "pid eq 4444" /nh /fo:csv ') do echo %%~a 

In this case, the list of processes is retrieved without headers (/nh) in csv format (/fo:csv). The commas are used as token delimiters and the first token in the line is the image name

note: In some windows versions (one of them, my case, is the spanish windows xp version), the pid filter in the tasklist does not work. In this case, the filter over the list of processes must be done out of the command

for /f "delims=," %%a in ('     tasklist /fo:csv /nh ^| findstr /b /r /c:"[^,]*,\"4444\"," ') do echo %%~a 

This will generate the task list and filter it searching for the process id in the second column of the csv output.

edited: alternatively, you can suppose what has been made by the team that translated the OS to spanish. I don't know what can happen in other locales.

tasklist /fi "idp eq 4444"  
like image 121
MC ND Avatar answered Oct 12 '22 08:10

MC ND


Using only "native" Windows utilities, try the following, where "516" is the process ID that you want the image name for:

for /f "delims=," %a in ( 'tasklist /fi "PID eq 516" /nh /fo:csv' ) do ( echo %~a ) for /f %a in ( 'tasklist /fi "PID eq 516" ^| findstr "516"' ) do ( echo %a ) 

Or you could use wmic (the Windows Management Instrumentation Command-line tool) and get the full path to the executable:

wmic process where processId=516 get name wmic process where processId=516 get ExecutablePath 

Or you could download Microsoft PsTools, or specifically download just the pslist utility, and use PsList:

for /f %a in ( 'pslist 516 ^| findstr "516"' ) do ( echo %a ) 
like image 35
Craig Tullis Avatar answered Oct 12 '22 07:10

Craig Tullis