Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

im trying to find a way to get my own PID from a command prompt (for later use in bat scripts) so far the only useful way i found was to use getpids.exe from here : http://www.scheibli.com/projects/getpids/index.html but im looking for a command that's "built in" to windows

edit : im looking for a "bullet proof" way - no assumptions about my process being the only cmd.exe or anything

share|improve this question
    
Are you looking for a command-line tool that displays the PID of the CMD.exe you are running it in? –  uSlackr Mar 26 '10 at 14:06
add comment

5 Answers

up vote 5 down vote accepted

since none of the above are bullet proof and builtin, I figured I'd add my little routine that closer to bullet proof but you'll need to parse/save the results somehow

do this if you can within the batch file

title=mycmd

tasklist /v /fo csv | findstr /i "mycmd"

share|improve this answer
    
thats really neat. im pretty sure the current time + some randomly-generated number can be set as the title to get near bulletproof –  radai Mar 29 '10 at 10:49
add comment

This should do the trick:

tasklist /v

share|improve this answer
add comment
  1. Windows Task Manager, you will need to go to View -> Select Columns.. and select PID.
  2. "tasklist /v" to get verbose task information in command prompt.
  3. Process Explorer from live.sysinternals.com.
share|improve this answer
    
@Vivek, from a command prompt... and you are duping answers –  WooYek Mar 27 '10 at 0:02
    
i just gave all possible options.. and no duplicating.. i could have also mentioned PowerShell but i restrained myself as that does not comes by default in XP and 2003. If this is for Vista onwards OS, Yes PowerShell would have been my preference. –  Vivek Kumbhar Mar 27 '10 at 0:24
add comment

If you've got the Windows 2003 Resource kit push it through qgrep to get just the line you want. You could then extract the pid from here (this assumes you've only got one cmd running at a time),

tasklist /v | qgrep cmd

cmd.exe 2040 RDP-Tcp#447 0 1,804 K Running MACHINE\Administrator  0:00:00 Command Prompt
share|improve this answer
add comment

Take a look at this little batch trick. It sets the title of cmd to a special value then uses tasklist to find it. Inventive

\\Greg

share|improve this answer
add comment

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.