Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm looking for something like chp.exe, it can runs a command and returns its pid. I can not use CHP.EXE because it seems to be reported as infected file in some antiviruses.


share|improve this question
You can run the program and parse the output of tasklist. – Karan May 20 '13 at 15:43
up vote 3 down vote accepted
Wmic process where (Name like '%CHP%') get caption, name, commandline, ProcessId | more

real sample:

Wmic process where (Name like '%ie%') get caption, name, commandline, ProcessId | more


ProcessId where like name Wmic Far manager

Wmic process get ProcessId

extract only the processId into a variable from query output:


@ECHO OFF                                                                              
FOR /F %%T IN ('Wmic process where^(Name^="explorer.exe"^)get ProcessId^|more +1') DO (
SET /A ProcessId=%%T) &GOTO SkipLine                                                   
echo ProcessId = %ProcessId%                                                           


ProcessId = 2372    
share|improve this answer
any suggestion to extract only the processId into a variable from query output? – Tobia May 28 '13 at 7:22
@Tobia see again – STTR May 28 '13 at 9:44

This is pretty straightforward to do in PowerShell. Start-Process notepad.exe -PassThru will start Notepad and return:

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName                                                                                                                              
-------  ------    -----      ----- -----   ------     -- -----------                                                                                                                              
     45       6     1672       4240    61     0.02   9212 notepad   

From there you could use it like an object by storing the output ($notepad = Start-process notepad.exe -PassThru and then $notepad.ID) and finish whatever scripting you were doing with it.

Getting it back to a batch script (if absolutely necessary) is a little tricky. If you're absolutely stuck on using a batch file, it would probably be easiest to write a PowerShell script that writes the relevant information to a file or registry key, call the PowerShell script from the batch file, and then read the file or reg key later in the batch script.

If you don't want to touch PowerShell at all then you'll have to resort to WMIC or tasklist to find the process after creating it.

share|improve this answer

you can run the program and then issue this command, substituting firefox with the process name you want:

for /f "tokens=2" %a in ('tasklist /nh /fi "imagename eq firefox.exe"') do echo %a

it will output the pid solely, in my case, the output is 5540

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .