Windows – How to find process pid of an application

batchpidprocesswindows 7

I have portable version of an app. I have ran for example 5 multiple instances of it and they all have the same process name but different pids. I want to find a way to for example kill one specific process, cause using taskkill with the name of that process would kill all of those. I want to kill just the specific one by providing the pid of that process.
Now the question is: How can I find a process pid so that I can use this number to kill that specific application easily?
For example I want to kill the third one (I mean by using time).
Can pid gives me information of when a process was ran? If not What are the other workarounds?

Best Answer

The multiple instances all have different PIDs as you've stated, so why do you need to change them? Just use TaskKill to kill on the basis of specific PIDs instead of the process name. From TaskKill /?:

/PID  processid        Specifies the PID of the process to be terminated.
                       Use TaskList to get the PID.

Edit: Here's a batch file that figures out the PID of the program instance it launched and thus can naturally be extended to kill that instance using TaskKill if so required:

@echo off
cls
set pidlstold=
set pidlstnew=
setlocal enabledelayedexpansion
for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a.!pidlstold!
if "!pidlstold!"=="No." (
    echo No running instance of Paint found. Launching Paint...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstold=%%a
    echo PID of just launched Paint instance is "!pidlstold!".
) else (
    echo One or more running instances of Paint found. Launching Paint again...
    start /min mspaint
    echo.
    for /f "tokens=2" %%a in ('tasklist /nh /fi "imagename eq mspaint*"') do set pidlstnew=%%a.!pidlstnew!
    set pidlstnew=!pidlstnew:%pidlstold%=!
    set pidlstnew=!pidlstnew:~0,-1!
    echo PID of just launched Paint instance is "!pidlstnew!".
)
Related Question