Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

Thanks

share|improve this question
1  
You can run the program and parse the output of tasklist. –  Karan May 20 '13 at 15:43

2 Answers 2

up vote 1 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

Output:

ProcessId where like name Wmic Far manager

Wmic process get ProcessId

extract only the processId into a variable from query output:

ProcID.cmd:

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

Output:

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.