Sergey V. Pereslavtsev
Sergey V. Pereslavtsev

Reputation: 661

How to get PID of process just started from within a batch file?

In Windows batch scripting there is start command which starts a new process.

Is it possible to get PID of the process just started?

Upvotes: 50

Views: 111096

Answers (7)

@ECHO OFF
SETLOCAL EnableDelayedExpansion EnableExtensions
::
::weil es mehrere Sessions für UltraCompare gleichzeitig geben kann, es wird hier die
::neueste Instanz (soeben gestartet) ermittelt, um später mit ProcessId diese Instanz 
::aktivieren zu können...
::
set "CreationDate="
set /A "ProcessIdUltraCompare=0"
::
set /A "lineno=0"
FOR /F %%T IN ('Wmic process where^(Name^="uc.exe"^) get CreationDate^|sort /r') DO (
set /A lineno+=1
rem echo %%T
rem echo !lineno!
if !lineno! equ 2 (
    rem ECHO %%T   
    set CreationDate=%%T
    rem echo !CreationDate!
    set /A "lineno=0"
    FOR /F %%P IN ('Wmic process where^(CreationDate^="!CreationDate!"^) get ProcessId') DO (
          set /A lineno+=1
          rem echo %%P
          if !lineno! equ 2 (
              set "ProcessIdUltraCompare=%%P"
              rem echo ProcessIdUltraCompare=!ProcessIdUltraCompare!
              goto :l_pid_uc_got
          )
      )
 )
)  
:l_pid_uc_got
    echo ProcessIdUltraCompare=!ProcessIdUltraCompare!
PAUSE

Upvotes: -1

nk125
nk125

Reputation: 1

This is the code that i use to get a PID

for /f "tokens=2 delims=," %%a in ('tasklist /FO CSV ^| findstr /I /C:"entertheprocess.here"') do (
    echo PID:%%a
)

Upvotes: 0

npocmaka
npocmaka

Reputation: 57252

you can try with

wmic process call create "notepad"

which will return the pid of the created process.

Processing this with FOR

setlocal
set "ReturnValue="
set "ProcessId="
for /f "eol=} skip=5 tokens=1,2 delims=;= " %%a in ('wmic process call create "notepad"') do (
    set "%%a=%%b"
)
echo %ReturnValue%
echo %ProcessId%
endlocal

Upvotes: 5

frog.ca
frog.ca

Reputation: 732

PowerShell can be used for this:

powershell -executionPolicy bypass -command "& {$process = start-process $args[0] -passthru -argumentlist $args[1..($args.length-1)]; exit $process.id}" notepad test.txt

echo Process ID of new process: %errorlevel%

Upvotes: 4

zappee
zappee

Reputation: 22668

This is an old post but I think that it worth to share the following 'easy to use' solution which works fine nowadays on Windows.

Start multiple processes in parallel:

start "<window title>" <command will be executed>

Example:

start "service1" mvn clean spring-boot:run
start "service2" mvn clean spring-boot:run

Obtain the PID of the processes (optional):

tasklist /V /FI "WindowTitle eq service1*"
tasklist /V /FI "WindowTitle eq service2*"

Kill the processes:

taskkill /FI "WindowTitle eq service1*" /T /F
taskkill /FI "WindowTitle eq service2*" /T /F

Upvotes: 40

Oliver Zendel
Oliver Zendel

Reputation: 2901

If there are processes already running with the same name, you first need to get a list of the current pids, than start your local process(es) and then check the pids again. Here is a sample code that starts 3 process and kills them at the end (specifically the ones started locally):

@echo off
set PROCESSNAME=notepad.exe

::First save current pids with the wanted process name
setlocal EnableExtensions EnableDelayedExpansion
set "RETPIDS="
set "OLDPIDS=p"
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (set "OLDPIDS=!OLDPIDS!%%ap")

::Spawn new process(es)
start %PROCESSNAME%
start %PROCESSNAME%
start %PROCESSNAME%

::Check and find processes missing in the old pid list
for /f "TOKENS=1" %%a in ('wmic PROCESS where "Name='%PROCESSNAME%'" get ProcessID ^| findstr [0-9]') do (
if "!OLDPIDS:p%%ap=zz!"=="%OLDPIDS%" (set "RETPIDS=/PID %%a !RETPIDS!")
)

::Kill the new threads (but no other)
taskkill %RETPIDS% /T > NUL 2>&1
endlocal

Upvotes: 15

Andy Arismendi
Andy Arismendi

Reputation: 52609

You can in batch but not directly per say. You need to either parse the output of tasklist.exe or use wmic.exe. Both require you to know what you just started which of course you will.

Using tasklist.exe:

for /F "TOKENS=1,2,*" %a in ('tasklist /FI "IMAGENAME eq powershell.exe"') do set MyPID=%b
echo %MyPID%

To use this in a batch script double up the percent signs.

Using wmic.exe:

for /f "TOKENS=1" %a in ('wmic PROCESS where "Name='powershell.exe'" get ProcessID ^| findstr [0-9]') do set MyPID=%a
echo  %MyPID%

Upvotes: 18

Related Questions