{"id":655,"date":"2014-09-19T00:01:00","date_gmt":"2014-09-19T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/19\/use-powershell-to-repeatedly-terminate-specific-processes\/"},"modified":"2014-09-19T00:01:00","modified_gmt":"2014-09-19T00:01:00","slug":"use-powershell-to-repeatedly-terminate-specific-processes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-repeatedly-terminate-specific-processes\/","title":{"rendered":"Use PowerShell to Repeatedly Terminate Specific Processes"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to terminate processes that start at random times.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I have a question that I hope you can answer. It seems that there are several processes that keep starting on my computer running Windows&nbsp;8.1. That eat up lots of resources, and I cannot seem to find where they are coming from. I have used the <a href=\"http:\/\/technet.microsoft.com\/en-us\/sysinternals\/bb963902.aspx\" target=\"_blank\">Sysinternals autoruns<\/a> command, and yet they keep coming and consuming. What I would really love is to have a script that detects when these bogus processes start, and then terminate them. Can you help with that?<\/p>\n<p>&mdash;CF<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello CF,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning it is Friday. WooHoo, the weekend baby! Actually, my weekends are pretty much like my weekdays, so I am not really one of those people who lives for the weekend. Having said that, it is still nice to have a weekend once in a while.<\/p>\n<p>So I am sitting on the porch, sipping a cup of English Breakfast tea with a cinnamon stick and a bit of lemon grass in it. I am checking my email at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, and I ran across your email. The answer is, &quot;Yes, I can write a bit of Windows PowerShell script that will detect when a specific process starts, and once that process starts, I can terminate that specific process.&quot; The technique is amazingly simple with Windows PowerShell. (Using VBScript in the old days, this was a bit of a bear.)<\/p>\n<h2>Detecting a process and terminating it<\/h2>\n<p>There are three steps required to detect when a specific process starts and then terminate the process:<\/p>\n<ol>\n<li>Write a WQL query that uses the Win32_ProcessStartTrace WMI class with a filter for a specific process.<\/li>\n<li>Register to receive events by using the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/jj590757.aspx\" target=\"_blank\">Register-CimIndicationEvent<\/a> Windows PowerShell cmdlet. In the command, use the query and specify an action to stop the process.<\/li>\n<li>Use the <b>Wait-Event<\/b> cmdlet to wait until an event triggers the action.<\/li>\n<\/ol>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> Today&#039;s blog post builds on concepts that I discussed yesterday in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-monitor-specific-process-creation\/\" target=\"_blank\">Use PowerShell to Monitor Specific Process Creation<\/a>, so you should review that post first. <br \/>&nbsp; &nbsp;T<span style=\"font-size:12px\">o work properly, the Windows PowerShell console must launch with Admin rights. Right-click the Windows PowerShell console icon, and select &ldquo;Run As Administrator.&rdquo;<\/span><\/p>\n<h2>Develop the query<\/h2>\n<p>To develop my query, I use WMI Query Language (WQL) and I query the Win32_ProcessStartTrace WMI class. This class is designed especially to detect when a new process begins, and therefore, it is easy to use. I use the <b>Where<\/b> clause to specify the <b>ProcessName<\/b> I want to detect. This requires that I specify the program file extension (unlike <b>Get-Process<\/b>, which only uses the base name). Here is the query I use:<\/p>\n<p style=\"margin-left:30px\">$Q = &quot;Select * from win32_ProcessStartTrace where processname = &#039;notepad.exe&#039;&quot;<\/p>\n<h2>Register for events<\/h2>\n<p>Now I want to register for events. In addition, I want to specify an action&mdash;that is, I want to stop the process when it launches. To do this, I will use an automatic variable, <b>$event<\/b>, that is generated when an event occurs. I use the <b>$event<\/b> variable to drill into the object. I go into <b>SourceEventArgs<\/b>, grab the <b>NewEvent<\/b> property and select the <b>ProcessID<\/b>. This is the program PID, and I pass it to the <b>Stop-Process<\/b> cmdlet. When a new instance of Notepad launches, within a short period of time, it is terminated. Here is the script:<\/p>\n<p style=\"margin-left:30px\">Register-CimIndicationEvent -Query $q -SourceIdentifier Q -Action {Stop-Process $event.SourceEventArgs.newevent.processID}<\/p>\n<h2>Wait for it&hellip;<\/h2>\n<p>Now I need to wait for an event to arise. To do this, I use the <b>Wait-Event<\/b> cmdlet. This cmdlet pauses the Windows PowerShell console and waits for events to arise. When I no longer want this to happen, I use Ctrl-C to break out of the wait cycle. Nothing appears on the screen while it is waiting or when a process terminates. The Windows PowerShell console is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-19-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-19-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>When I break the <b>Wait-Event<\/b> cycle, Windows PowerShell no longer terminates the process. But if I start up again, <b>Wait-Event<\/b> (prior to removing the subscription) will continue to work. When I close Windows PowerShell, everything is reset to defaults.<\/p>\n<p>CF, that is all there is to using Windows PowerShell to terminate processes that keep starting. Join me tomorrow when we will have another great guest blog post by Windows PowerShell MVP, Sean Kearney.<\/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\" target=\"_blank\">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><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to terminate processes that start at random times. &nbsp;Hey, Scripting Guy! I have a question that I hope you can answer. It seems that there are several processes that keep starting on my computer running Windows&nbsp;8.1. That eat up lots of resources, and I [&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":[385,42,31,87,3,4,45,6],"class_list":["post-655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-cim","tag-events-and-monitoring","tag-operating-system","tag-processes","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to terminate processes that start at random times. &nbsp;Hey, Scripting Guy! I have a question that I hope you can answer. It seems that there are several processes that keep starting on my computer running Windows&nbsp;8.1. That eat up lots of resources, and I [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/655","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=655"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/655\/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=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}