{"id":13051,"date":"2011-08-10T00:01:00","date_gmt":"2011-08-10T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/08\/10\/use-powershell-and-wmi-to-terminate-multiple-processes\/"},"modified":"2011-08-10T00:01:00","modified_gmt":"2011-08-10T00:01:00","slug":"use-powershell-and-wmi-to-terminate-multiple-processes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-and-wmi-to-terminate-multiple-processes\/","title":{"rendered":"Use PowerShell and WMI to Terminate Multiple Processes"},"content":{"rendered":"<p><strong>Summary<\/strong>: Learn how to use Windows PowerShell and WMI to terminate multiple processes.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" 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\" \/>Hey, Scripting Guy! I am wondering about how to use WMI to work with multiple instances of a class. For example, if I have 50 processes running, and I want to kill all of them at the same time, I can easily do this using the <b>Get-Process<\/b> and <b>Stop-Process<\/b> cmdlets. What if I want to use WMI instead? What do I need to do?<\/p>\n<p>&mdash;CT<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" 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\" \/>Hello CT,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. <a href=\"http:\/\/powershellgroup.org\/corpus.tx\">Last night&rsquo;s PowerShell user group meeting in Corpus Christi, Texas<\/a> was a lot of fun. This was their first meeting, and Marc Adam Carter, the chapter president did an awesome job putting things together! It is a lot of work to start a PowerShell users group, and I really appreciate his enthusiasm. The Scripting Wife and I really enjoyed speaking and hanging out with people who share our passion for Windows PowerShell. If you ask her (the Scripting Wife that is), she will tell you my favorite two things are Windows PowerShell and talking to IT pros. When I combine the two activities, I am a little hard to control. Once again, the <a href=\"https:\/\/www.youtube.com\/user\/ye110wbeard#p\/u\/12\/Z42M8GT4lSc\">Highway to PowerShell<\/a> rocked the walls and heralded my presentation.<\/p>\n<p>Suppose I have fifty copies of Notepad running&mdash;that&rsquo;s right, fifty. Don&rsquo;t ask what I am doing with fifty copies of Notepad running. It just seems to happen at times, particularly if I use the <b>range<\/b> operator, a <b>ForEach-Object<\/b> statement, and call Notepad. Now, if I use WMI and the <b>Win32_Process<\/b> class, I can filter out all processes except for Notepad and store the results in a variable. I can then use the <b>count<\/b> property to see how many instances of Notepad are running. Now, if I attempt to call the <b>Terminate<\/b><i> <\/i>method, it will fail. But, my first indication that things are somewhat awry is that tab expansion for the <b>Terminate<\/b> method does not work. The reason for the failure is that I am dealing with an array instead of a single instance of a process. The commands I have used are shown here:<\/p>\n<p style=\"padding-left: 30px\">1..50 | % {notepad}<\/p>\n<p style=\"padding-left: 30px\">$a = Get-WmiObject win32_process -Filter &#8220;name = &#8216;notepad.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">$a.Count<\/p>\n<p style=\"padding-left: 30px\">$a.terminate()<\/p>\n<p>&nbsp;<\/p>\n<p>The commands and the associated output are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5164.HSG-8-10-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5164.HSG-8-10-11-01.png\" \/><\/a><\/p>\n<p>If I want to terminate all 50 instances of Notepad, I can use the instances that are stored in the <b>$a<\/b> variable, but I will need to walk through the array and call the <b>Terminate<\/b> method once for each instance of the process. To do this, I find it easiest to use the pipeline, and the <b>Foreach-Object<\/b> cmdlet to permit me to work with each instance. The <b>Foreach-Object<\/b> cmdlet has the <b>%<\/b> alias; therefore, it is really easy to use at the command line inside the Windows PowerShell console. Because I am going to terminate 50 processes, I do not want to clutter my console with 50 return codes, so I pipe the output to the <b>Out-Null<\/b> cmdlet (to discard the return values). After I have terminated the 50 processes, I use the <b>Get-WmiObject<\/b> cmdlet to query once again for instances of notepad.exe. No error is returned if no instances of the process appear. These two commands are shown here:<\/p>\n<p style=\"padding-left: 30px\">$a | % { ([wmi]$_.__RELPATH).terminate() | out-null }<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_process -Filter &#8220;name = &#8216;notepad.exe'&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>The commands and the associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2045.HSG-8-10-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2045.HSG-8-10-11-02.png\" \/><\/a><\/p>\n<p>I can use the <b>Invoke-WmiMethod<\/b> cmdlet to terminate all 50 instances of Notepad. First, I create 50 instances of Notepad. Next, I use the <b>Get-WmiObject<\/b> cmdlet to retrieve all the instances of Notepad. Then I pipe the objects to the <b>Foreach-Object<\/b> cmdlet. In the <b>Invoke-WMIMethod<\/b> cmdlet, I use the <i>inputobject<\/i> parameter to receive the object upon which to work. The cmdlet is smart enough to retrieve the relative path upon which to work. The commands are shown here:<\/p>\n<p style=\"padding-left: 30px\">1..50 | % {notepad}<\/p>\n<p style=\"padding-left: 30px\">$a = Get-WmiObject win32_process -Filter &#8220;name = &#8216;notepad.exe'&#8221;<\/p>\n<p style=\"padding-left: 30px\">$a | % {Invoke-WmiMethod -Name terminate -InputObject $_ | out-null}<\/p>\n<p>The commands and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5023.HSG-8-10-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5023.HSG-8-10-11-03.png\" \/><\/a><\/p>\n<p>Of course, it is possible to simplify working with the <b>Terminate<\/b><i> <\/i>method. If I have to perform a WMI query to return all instances of the notepad.exe process, why store the results into a variable only to pipe the results and call a method? I can cut out the intermediary, and call the method directly. In this example, I first create 50 instances of Notepad, and then I use the <b>Get-WmiObject<\/b> cmdlet to find all instances of the notepad.exe process. I then pipe the results to the <b>ForEach-Object<\/b> cmdlet (using the <b>%<\/b> alias) and call the <b>Terminate<\/b> method from the piped object. I then write all 50 return codes back to the <b>$null<\/b> variable. The last <b>Get-WmiObject<\/b> command is used to show that no instances of Notepad are left running. The commands are shown here:<\/p>\n<p style=\"padding-left: 30px\">1..50 | % {notepad}<\/p>\n<p style=\"padding-left: 30px\">$null = Get-WmiObject win32_process -Filter &#8220;name = &#8216;notepad.exe'&#8221; | % {$_.Terminate() }<\/p>\n<p style=\"padding-left: 30px\">Get-WmiObject win32_process -Filter &#8220;name = &#8216;notepad.exe'&#8221;<\/p>\n<p>&nbsp;<\/p>\n<p>The commands and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1731.HSG-8-10-11-04.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1731.HSG-8-10-11-04.png\" \/><\/a><\/p>\n<p>I can use exactly the same technique to work with multiple processes. I modify the command that creates 50 instances of Notepad, so that it also creates 50 instances of the calc.exe process. Next, I modify the WMI <i>filter <\/i>parameter so that it returns all instances of both Notepad and Calculator. The remainder of the command is exactly the same: I pipe the resulting WMI objects to the <b>ForEach-Object<\/b> cmdlet, and I call the <b>Terminate<\/b><i> <\/i>method of each object as it crosses the pipeline. I store the return codes in the <b>$null<\/b> variable and therefore discard them and avoid cluttering the Windows PowerShell console. The commands are shown here (the second command is a single logical command; it is broken at the pipeline character for better display on this blog):<\/p>\n<p style=\"padding-left: 30px\">1..50 | % {notepad;calc}<\/p>\n<p style=\"padding-left: 30px\">$null = Get-WmiObject win32_process -Filter &#8220;name = &#8216;notepad.exe&#8217; OR name = &#8216;calc.exe'&#8221; |<\/p>\n<p style=\"padding-left: 30px\">% { $_.Terminate() }<\/p>\n<p>There is no output from the above commands, and therefore no need for another screen shot.<\/p>\n<p>&nbsp;<\/p>\n<p>CT, that is all there is to using WMI instance methods when working with multiple instances of the WMI class. WMI Method Week will continue tomorrow when I will continue talking about working with WMI methods.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to use Windows PowerShell and WMI to terminate multiple processes. &nbsp; Hey, Scripting Guy! I am wondering about how to use WMI to work with multiple instances of a class. For example, if I have 50 processes running, and I want to kill all of them at the same time, I can [&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":[3,4,45,6],"class_list":["post-13051","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to use Windows PowerShell and WMI to terminate multiple processes. &nbsp; Hey, Scripting Guy! I am wondering about how to use WMI to work with multiple instances of a class. For example, if I have 50 processes running, and I want to kill all of them at the same time, I can [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13051","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=13051"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/13051\/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=13051"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=13051"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=13051"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}