Windows batch file : PID of last process

batch-filebrowserpidwindows

I am launching a browser from batch file.

START "www.google.com"

I would like to know the PID of this browser window launched.

There can be many browser windows launched on a single machine. I need to find the PID of the process which was launched by my batch file only. I tried with WINDOWTITLE filter. But its not a good idea as titles may change in future. I am using Windows XP/7

Any help would be appreciated.
Thanks.

Best Answer

For what it worth (question is more than 2 years old) this code do the trick, just change variable according to default browser exe

set "browser=palemoon.exe"

tasklist /FI "imagename eq %browser%" /NH /FO csv > task-before.txt
start www.google.com
tasklist /FI "imagename eq %browser%" /NH /FO csv > task-after.txt

:: fc /L /LB1  test4-Before.txt test4-After.txt | find /I "%browser%"
for /f "delims=, tokens=2,*" %%A in ('"fc /L /LB1  task-before.txt task-after.txt | find /I "%browser%""') do set pid=%%A

SETLOCAL enabledelayedexpansion
pid=!pid:"=!
ENDLOCAL
echo pid is %pid%