How to find process pid of an application

16,270

Solution 1

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!".
)

Solution 2

You can do this with powershell. For example, this lists all the processes named "chrome" on my machine and includes their starttime, sorted by their start time.

PS C:\Users\Zach> Get-Process chrome | Select-Object name,id,starttime

Name       Id StartTime
----       -- ---------
chrome   1752 4/16/2015 5:53:05 PM
chrome   5404 4/16/2015 5:53:05 PM
chrome   3980 4/16/2015 5:53:05 PM
chrome   4784 4/16/2015 5:53:05 PM
chrome   4336 4/16/2015 5:53:05 PM
chrome   4492 4/16/2015 5:53:05 PM
chrome   8812 4/16/2015 5:58:19 PM
chrome   9908 4/16/2015 5:58:19 PM
chrome   3608 4/16/2015 5:58:19 PM
chrome   9980 4/16/2015 5:58:19 PM
chrome   8536 4/16/2015 5:58:19 PM
chrome   9664 4/16/2015 5:58:19 PM
chrome   1700 4/16/2015 6:02:26 PM
chrome   9712 4/16/2015 7:50:31 PM
chrome   5920 4/16/2015 7:50:31 PM
chrome   4572 4/16/2015 7:50:31 PM
chrome   2400 4/17/2015 6:19:58 AM
chrome  11780 4/21/2015 7:17:22 PM
chrome  11340 4/21/2015 8:19:17 PM
chrome   7828 4/21/2015 8:19:19 PM
chrome   9448 4/21/2015 8:32:05 PM
chrome   3400 4/22/2015 7:48:21 PM
chrome   8860 4/22/2015 7:53:28 PM
chrome  10364 4/24/2015 1:04:54 AM
chrome   4596 4/24/2015 9:52:24 AM
chrome  13392 4/24/2015 10:39:31 AM
chrome  14596 4/24/2015 10:44:28 AM
chrome   3252 4/24/2015 10:49:28 AM
chrome  16100 4/24/2015 11:31:08 AM
chrome  13840 4/24/2015 3:14:34 PM
chrome   4472 4/24/2015 6:31:11 PM
chrome  13652 4/24/2015 6:31:31 PM
chrome  12008 4/25/2015 12:51:15 AM
chrome  16016 4/25/2015 12:51:17 AM
chrome   9852 4/25/2015 1:49:19 PM
chrome  14548 4/25/2015 2:10:19 PM
chrome  16364 4/25/2015 9:18:12 PM
chrome  13860 4/26/2015 12:21:57 AM
chrome  13004 4/26/2015 10:07:53 AM
chrome   1364 4/26/2015 10:13:30 AM
chrome  12464 4/26/2015 10:13:38 AM
chrome  15144 4/26/2015 10:13:52 AM
chrome   1040 4/26/2015 10:28:49 AM
chrome  15800 4/26/2015 10:38:00 AM
chrome  12984 4/26/2015 2:51:06 PM
chrome  12972 4/26/2015 7:18:05 PM
chrome  11816 4/26/2015 7:24:32 PM
chrome  15044 4/27/2015 6:54:53 PM
chrome  11916 4/27/2015 8:00:45 PM
chrome  16216 4/28/2015 12:45:07 AM
chrome  11404 4/28/2015 8:09:43 PM
chrome  15680 4/28/2015 8:57:05 PM
chrome   6864 4/28/2015 9:12:56 PM
chrome  16172 4/28/2015 11:34:43 PM
chrome  10432 4/28/2015 11:56:50 PM
chrome  14856 4/29/2015 12:43:49 AM
chrome  17372 4/29/2015 12:45:57 AM

Using this, you can find the pid of the one you want to kill. For example:

PS C:\Users\Zach> Stop-Process 3980

to stop the 3rd one in the list.

Solution 3

If you want a quick way to find the PID of a program using only the built-in tools, you can do all this with Windows Task Manager.

  • On the Applications tab and find the application name. If you aren't sure which one is the correct one, then right click it and choose Bring To Front. If the right one comes to the front, then you have the correct entry.
  • Right click the application name and choose Go to Process.
  • Optionally, if you want the PID displayed, go to View --> Columns and make sure that PID (Process Identifier) is selected.
  • Right click the process that is highlighted, and choose End process.
Share:
16,270

Related videos on Youtube

Admin
Author by

Admin

Updated on September 18, 2022

Comments

  • Admin
    Admin over 1 year

    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?

    • Felype
      Felype about 9 years
      That is not possible under windows (and i don't think its possible under linux and unix either), but you can get and kill windows filtering by process name, main window handler, window class and some other methods.
    • Karan
      Karan about 9 years
      You are asking the wrong question. Since the instances already have different PIDs (as you've yourself stated), there's no need to change these PIDs when you just want to kill certain instances. See my answer below.
    • Admin
      Admin about 9 years
      @Karan Lets make this interesting. Assume I want to do this from a batch file (automate things and schedule them) and I dont know what is the pid of that process (and again there are multiple instances with same names).
    • Scott - Слава Україні
      Scott - Слава Україні about 9 years
      Perhaps the question you should be asking is "When I start an application (e.g., from a batch file), how can the batch script learn the PID of the application process, so it can use it later to kill the process?"  (The answer might be: run tasklist with output to a file before and after starting the application, then compare the output files to see what PID is in the second file but not the first.)  Or perhaps, "Given multiple Windows processes, how can I distinguish among them (e.g., by start time or other discriminators)?"
    • Scott - Слава Україні
      Scott - Слава Україні about 9 years
      Also, (1) You might want to tag your question with [windows] (ideally, specifying the exact version that you're using) and [batch], because, until you edited the question, there was no way to tell that you weren't talking about Unix.  Also, (2) I don't understand what you mean by "I have an application installed on my computer ... and I have the portable version of that too."  If that's important to your question, you might want to reword it.
    • Karan
      Karan about 9 years
      @MrKlKl: Here you go, see the batch file in my edited answer below. Also as Scott mentioned above you really should edit the question title, tags and probably the body as well to remove all references to setting PIDs.
    • Admin
      Admin about 9 years
      Ok @Scott By Installation I meant that the application leaves traces (registery etc.) which the portable dont. I thought this make a difference between installed and portable processes of an application. which now I think dont.
  • Felype
    Felype about 9 years
    I'm not the one to downvote, but that answer seems a bit off the question. The question was: "How to change the PID of a process".
  • Karan
    Karan about 9 years
    @Felype: Read my answer. It's an X-Y question. The OP is trying to kill using the process name, but doesn't need to since taskkill can already kill using specific PIDs. What he wants to do (change PIDs) is useless for the purpose of killing instances since they already have different PIDs by his own admission.
  • Felype
    Felype about 9 years
    Yes I understand, present him the other filters, he clearly stated to know how to kill by PID, present now the /f method to filter by imagename, window name, etc. Again, I didn't downvote.
  • Karan
    Karan about 9 years
    @Felype: I'm not accusing you of being the downvoter. All I'm saying is that clearly the OP's on the wrong track and my answer is relevant, so the downvote is IMO stupid unless the voter can give me a good reason for it.
  • Felype
    Felype about 9 years
    Indeed. I just said what I think it might be. The OP did mention he knows how to taskkill /pid, so this answer doesn't answer at all, even if the question may sound stupid.
  • Karan
    Karan about 9 years
    "The OP did mention he knows how to taskkill /pid" - No, he did not. Read the question carefully again. He says he's trying to kill by using the process name, and that "I want to kill just the specific one by providing the pid of that process." So clearly he doesn't know how.
  • Ramhound
    Ramhound about 9 years
    This seems more like a comment. There is an answer to this question, it might not involve the approach the author wants, but there is an answer to this question.
  • Jamie Hanrahan
    Jamie Hanrahan about 9 years
    Beg pardon, but the explicitly stated question was: "Now the question is [note wording]: How can I set a process pid?" The answer is : You can't.
  • Admin
    Admin about 9 years
    This kills all processes with PID above 1000 What do you mean by that? Do pid numbers go up step by step when a new program is ran?
  • Scott - Слава Україні
    Scott - Слава Україні about 9 years
    @Karan: I agree that it’s an XY problem, but I think you’ve got the X wrong.  I think X is, “If a.bat says (as a hypothetical, illustrative example) start calc, how can it later terminate the calc process that it started, given that b.bat and c.bat have also started their own instances of calc (and that we don’t want to kill them)?” (Or something like that.)  (Y is, “How can a.bat assign a pid of 1001 to its calc, so it can later kill its calc process by using that pid?”  That suggests that the OP does know how to taskkill /pid.)
  • Felype
    Felype about 9 years
    not really, The Operating System defines PID in a very 'magical' way, not sequential and sometimes a program opened later will have a PID with lower value than the program opened earlier. That is just a "form of filtering" you can filter PIDs between X and Y, but I think that won't fit for your needs, check /f /immethod instead.
  • Karan
    Karan about 9 years
    @Scott: Meh, let's ignore for the moment what the OP knows or doesn't know about taskkill. :) Going by his latest comment though I do seem to have figured out what he wants, hence my edit above.
  • Scott - Слава Україні
    Scott - Слава Україні about 9 years
    @Karan: Ah, basically a streamlined version of what I suggested in my comment.  How do we know that tasklist will list processes in order of creation?
  • Karan
    Karan about 9 years
    @Scott: That does seem to be the case. If there's any chance at all that it will return the PIDs in random order, the code can be altered to take each PID from pidlstold and delete it from pidlstnew till only one is left. So far I don't think that's required.