How Can I Terminate a Process with a Specific PID?

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.