How Can I Terminate a Process with a Specific PID?

How Can I Terminate a Process with a Specific PID?

  • Comments 4
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! If I have the PID, is there a way to kill a process using a script?

-- JV

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JV. You bet there is. For those of you who aren’t familiar with the acronym, PID is short for Process Identifier, a unique ID number assigned to a process when it gets created. (Well, temporarily unique: as soon as the process is destroyed, the number is recycled and can be assigned to another new process.) The WMI class Win32_Process has a property - ProcessId - that corresponds to the PID. Suppose you want to terminate a process that has a PID of 2576. Here’s a script that will do that very thing:

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where ProcessID = 2576")
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

Of course, keep in mind that PIDs are ever-changing: although the process you want to terminate today might have a PID of 2576, the odds are that it will not have the same PID the next time it gets created. Therefore, instead of hard-coding in the PID value, you might want to modify the script so that it can accept any PID as a command-line argument:

If Wscript.Arguments.Count = 0 Then
    Wscript.Echo "You must enter a PID."
    Wscript.Quit
End If

intPID = Wscript.Arguments.Item(0)

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where ProcessID = " & intPID & "")
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

In this script, we check to see if any command-line arguments were entered; if there aren’t any, we echo an error message (“You must enter a PID.”) and then quit. If an argument is entered, we assign the value of the first argument to the variable intPID using this code:

intPID = Wscript.Arguments.Item(0)

What if we enter more than one command-line argument? Well, in this simple script we just ignore additional arguments. But you could easily modify this script to terminate as many processes as you have PIDs for. For more information on how to do that, check out this Tales from the Script column.

After assigning a value to intPID we then use the Win32_Process class to locate the process and terminate it. Note that we don’t hard-code in the PID, but instead use the variable intPID:

("Select * from Win32_Process Where ProcessID = " & intPID & "")

And while you didn’t ask this, you can also terminate processes by name rather than PID. For example, this script terminates all the instances of Notepad.exe running on a computer:

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = 'Notepad.exe'")
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

Like we said, that terminates all instances of Notepad. If you want to terminate only a specific instance, use the PID. PIDs are unique to the collection of running processes; names are not. In other words, there will only be one process with a PID of 2576, but there could be scores of processes with the Name Notepad.exe.

For more information about managing processes using scripts, take a look at this portion of the Microsoft Windows 2000 Scripting Guide.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hey Scripting Guy! I'm trying to make a vbs script that stops explorer.exe running, comes up with an input box prompting you for a password, and when the correct password is entered, re runs explorer.exe and let's you into the computer. If the the incorrect password is entered five times, it takes a photo using a webcam and emails it to an email address while keeping that user out by keeping the input box there and explorer.exe still shutdown! It's alot to ask for, but I'm pretty stuck! Please help!

    - - F

  •  With objProcess.Terminate() some process don´t can close it, so to do it I must use the shell.run or cmd to use the taskkill, that it can close the process.

  • I have a website (that runs in a Garden) that sometimes gets a LOT of 500 errors. Is there a way to (1) determine the PID so it can be killed or (2) to issue a Application Pool recycle?

    I would need this to be done in the 500-error routine of the website - running under IUSR

  • Hey Scripting Guy!  I have termservice hanging on some remote servers.  I can use tasklist to get the pid and then use taskkill to kill Termservice.  Is there anyway to make this into a script file?