{"id":85945,"date":"2004-09-27T05:40:19","date_gmt":"2004-09-27T13:40:19","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/scripting\/?p=85945"},"modified":"2019-06-05T05:43:10","modified_gmt":"2019-06-05T13:43:10","slug":"how-can-i-terminate-a-process-with-a-specific-pid","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-terminate-a-process-with-a-specific-pid\/","title":{"rendered":"How Can I Terminate a Process with a Specific PID?"},"content":{"rendered":"<p><IMG class=nearGraphic title=\"Hey, Scripting Guy! Question\" border=0 alt=\"Hey, Scripting Guy! Question\" align=left src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=34 height=34> \n<P>Hey, Scripting Guy! If I have the PID, is there a way to kill a process using a script?<BR><BR>&#8212; JV<\/P><IMG border=0 alt=Spacer src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=5 height=5><IMG class=nearGraphic title=\"Hey, Scripting Guy! Answer\" border=0 alt=\"Hey, Scripting Guy! Answer\" align=left src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=34 height=34><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=farGraphic title=\"Script Center\" border=0 alt=\"Script Center\" align=right src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=120 height=288><\/A> \n<P>Hey, JV. You bet there is. For those of you who aren\u2019t 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 &#8211; ProcessId &#8211; that corresponds to the PID. Suppose you want to terminate a process that has a PID of 2576. Here\u2019s a script that will do that very thing:<\/P><PRE class=codeSample>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colProcessList = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process Where ProcessID = 2576&#8221;)\nFor Each objProcess in colProcessList\n    objProcess.Terminate()\nNext\n<\/PRE>\n<P>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 <I>not<\/I> 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:<\/P><PRE class=codeSample>If Wscript.Arguments.Count = 0 Then\n    Wscript.Echo &#8220;You must enter a PID.&#8221;\n    Wscript.Quit\nEnd If<\/p>\n<p>intPID = Wscript.Arguments.Item(0)<\/p>\n<p>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colProcessList = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process Where ProcessID = &#8221; &amp; intPID &amp; &#8220;&#8221;)\nFor Each objProcess in colProcessList\n    objProcess.Terminate()\nNext\n<\/PRE>\n<P>In this script, we check to see if any command-line arguments were entered; if there aren\u2019t any, we echo an error message (\u201cYou must enter a PID.\u201d) and then quit. If an argument <I>is<\/I> entered, we assign the value of the first argument to the variable intPID using this code:<\/P><PRE class=codeSample>intPID = Wscript.Arguments.Item(0)\n<\/PRE>\n<P>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 <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/tales\/sg0704.mspx\"><B>Tales from the Script<\/B><\/A> column.<\/P>\n<P>After assigning a value to intPID we then use the Win32_Process class to locate the process and terminate it. Note that we don\u2019t hard-code in the PID, but instead use the variable intPID:<\/P><PRE class=codeSample>(&#8220;Select * from Win32_Process Where ProcessID = &#8221; &amp; intPID &amp; &#8220;&#8221;)\n<\/PRE>\n<P>And while you didn\u2019t 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:<\/P><PRE class=codeSample>strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject _\n    (&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colProcessList = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Process Where Name = &#8216;Notepad.exe'&#8221;)\nFor Each objProcess in colProcessList\n    objProcess.Terminate()\nNext\n<\/PRE>\n<P>Like we said, that terminates <I>all<\/I> 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.<\/P>\n<P>For more information about managing processes using scripts, take a look at this portion of the <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_prc_overview.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A>.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! If I have the PID, is there a way to kill a process using a script?&#8212; JV Hey, JV. You bet there is. For those of you who aren\u2019t familiar with the acronym, PID is short for Process Identifier, a unique ID number assigned to a process when it gets created. (Well, [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-85945","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! If I have the PID, is there a way to kill a process using a script?&#8212; JV Hey, JV. You bet there is. For those of you who aren\u2019t familiar with the acronym, PID is short for Process Identifier, a unique ID number assigned to a process when it gets created. (Well, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/85945","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=85945"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/85945\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=85945"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=85945"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=85945"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}