{"id":54763,"date":"2008-12-03T11:31:00","date_gmt":"2008-12-03T11:31:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/12\/03\/hey-scripting-guy-how-can-i-kill-processes\/"},"modified":"2008-12-03T11:31:00","modified_gmt":"2008-12-03T11:31:00","slug":"hey-scripting-guy-how-can-i-kill-processes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-kill-processes\/","title":{"rendered":"Hey, Scripting Guy! How Can I Kill Processes?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/H2>\n<P>Hey, Scripting Guy! It seems that every single piece of software I install adds a stub application that starts automatically. I know software makers do not do it out of maliciousness, but good grief these people act as if theirs is the most important software on my computer. In the old days, it was pretty easy. I\u2019d go to the Startup folder and delete their icon. Now they start hiding their stuff in 25 different places in the registry. When people started finding their software in those locations, these software makers began masquerading as services with obscure names. It is enough to make a person remove patches of their own hair. Can you provide a low-stress, low-hair-loss way of controlling these offending applications?<BR><BR>&#8211; JB<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hi JB,<\/P>\n<P>I can definitely sympathize with <A href=\"http:\/\/en.wiktionary.org\/wiki\/follicle\" target=\"_blank\">follicly challenged<\/A> individuals, and do not wish to do anything that would cause you to remove more of the precious stuff. If you take a look at the script below, you will see that we read a text file that has a list of processes we wish to kill. We total the memory being consumed by the processes and ask if you, the user, wish to terminate them. For a similar VBScript, you can refer to <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/scriptshop\/shop0705.mspx\" target=\"_blank\">this Dr. Scripto article<\/A>. For more information about WMI, you may want to look at the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/wmifaq.mspx\" target=\"_blank\">WMI Secrets page<\/A> or check out the Microsoft Press book, <A href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth8853.aspx\" target=\"_blank\">Microsoft Windows Scripting with WMI: Self-Paced Learning Guide<\/A>. Here&#8217;s the&nbsp;script:<\/P><PRE class=\"codeSample\">$mem = 0\n$procFile = &#8220;c:\\FSO\\annoyingProcesses.txt&#8221; \nGet-process -name (Get-Content $procFile) -ErrorAction continue |\nForeach-Object { $mem += $_.WorkingSet }\n&#8220;{0:N2}&#8221;-f ($mem\/1MB) +&#8221; meg of ram is being consumed by $procFile Processes&#8221; \n$rtnPrompt = Read-Host -Prompt `\n  &#8221; \n   would like to kill $procfile processes? \n   &lt;y \/ n&gt;\n  &#8221;\nswitch($rtnPrompt) \n{\n &#8220;y&#8221;     { \n          Write-Host -BackgroundColor green &#8220;$procfile processes will be killed&#8230;&#8221; \n          stop-process -name $(Get-Content $procFile) -ErrorAction continue \n         }\n &#8220;n&#8221;     { \n          Write-Host -BackgroundColor yellow &#8220;$procfile processes will not be killed&#8221; \n         }\n default { \n           Write-Host -BackgroundColor red &#8220;Response y or n expected. exiting&#8230;&#8221; ;\n           exit\n         }\n} #end switch\n<\/PRE>\n<P>The script uses a text file for input. Working with text files in VBScript is <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/begin\/ss0207.mspx\" target=\"_blank\">covered here<\/A>. The first thing it does is initialize two variables. The first variable, <B>$mem<\/B>, is used to hold the amount of memory that will be saved by deleting the processes. The second variable, <B>$procFile<\/B>, is used to hold the path to the text file with the processes to&nbsp;kill:<\/P><PRE class=\"codeSample\">$mem = 0\n$procFile = &#8220;c:\\FSO\\annoyingProcesses.txt&#8221;\n<\/PRE>\n<P>The text file is simply a list of process names:<\/P><IMG height=\"334\" alt=\"Image of the list of process names in a text file\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1203\/hsg_kap01.jpg\" width=\"400\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>Next it is time to read the text file and retrieve the process information for each of the processes. This can be done on a single line. The trick is that the <B>Get-Process<\/B> cmdlet will accept an array of process names for the <B>\u2013name<\/B> parameter. To provide this array of process names, we use the <B>Get-Content<\/B> cmdlet and have it read the content from the file. Because we have the <B>Get-Content<\/B> cmdlet in parentheses, this forces it to be read first. The <B>\u2013ErrorAction<\/B> continue is the same as placing <B>On Error Resume Next<\/B> in a script. It causes error messages to be ignored. After we have the process information for each of the processes, we pipeline the results to the next command. This line of code is seen here:<\/P><PRE class=\"codeSample\">Get-Process -name (Get-Content $procFile) -ErrorAction continue |<\/PRE>\n<P>Because we have a stream of process information, we want to work with each process individually. To do this, we use the <B>Foreach-Object<\/B> cmdlet. We wish to tally up the <B>WorkingSet<\/B> property of each of the processes. To do this, we use the <B>$_<\/B> variable, which refers to the individual process object that is currently residing on the pipeline. We then query the <B>WorkingSet<\/B> value and add it to the current total of <B>$mem<\/B>. The <B>+=<\/B> construction is the same as saying <B>$mem = $mem + $_.WorkingSet<\/B>. This line of code is seen here:<\/P><PRE class=\"codeSample\">Foreach-Object { $mem += $_.WorkingSet }<\/PRE>\n<P>The next line of code is used to print out the amount of memory that is being consumed. <SPAN class=\"codeSample\">{0:N2}<\/SPAN> is a .NET framework format specifier that says we want to display numbers with two decimal places. The strange thing is that it appears in front of the <B>\u2013f<\/B> instead of behind it. <B>1MB<\/B> is called an administrative constant, and it is used to convert bytes to megabytes: <\/P><PRE class=\"codeSample\">&#8220;{0:N2}&#8221;-f ($mem\/1MB) +&#8221; meg of ram is being consumed by $procFile Processes&#8221;<\/PRE>\n<P>Here is how memory being consumed is displayed:<\/P><IMG height=\"272\" alt=\"Image of the display of memory being consumed\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1203\/hsg_kap02.jpg\" width=\"500\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>It is now time to show a prompt, so we can ensure the user intended to stop all the processes. To do this, we use the <B>Read-Host<\/B> cmdlet. This code is shown here (the prompt itself can be seen in the previous&nbsp;image):<\/P><PRE class=\"codeSample\">$rtnPrompt = Read-Host -Prompt `\n  &#8221; \n   would like to kill $procfile processes? \n   &lt;y \/ n&gt;\n  &#8221;\n<\/PRE>\n<P>Now we need to evaluate the user\u2019s response. To do this, we use the <B>switch<\/B> statement, which is similar to the <B>Select<\/B> case command from VBScript. The switch is used to determine what was handed to the <B>Read-Host<\/B> cmdlet. This value was stored in the <B>$rtnPrompt<\/B> variable. If it is equal to <B>y<\/B>, we print in green the processes that will be deleted, and then we call the <B>Stop-Process<\/B> cmdlet to stop each process. If the variable is equal to <B>n<\/B>, the message is printed in yellow to say that nothing will be stopped. If an invalid response is typed, the message is displayed in red that a <B>y<\/B> or <B>n<\/B> response was&nbsp;expected:<\/P><PRE class=\"codeSample\">switch($rtnPrompt) \n{\n &#8220;y&#8221;     { \n          Write-Host -BackgroundColor green &#8220;$procfile processes will be killed&#8230;&#8221; \n          stop-process -name $(Get-Content $procFile) -ErrorAction continue \n         }\n &#8220;n&#8221;     { \n          Write-Host -BackgroundColor yellow &#8220;$procfile processes will not be killed&#8221; \n         }\n default { \n           Write-Host -BackgroundColor red &#8220;Response y or n expected. exiting&#8230;&#8221; ;\n           exit\n         }\n} #end switch\n<\/PRE>\n<P>Here is what the display looks like when processes will be killed:<\/P><IMG height=\"299\" alt=\"Image of what is displayed when processes will be killed\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/hey1203\/hsg_kap03.jpg\" width=\"550\" border=\"0\"> \n<P>&nbsp;<\/P>\n<P>Well, JB, hopefully this script will alleviate some of your hair loss problems. If not, consult your general practitioner. Script you tomorrow.<\/P>\n<P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><SPAN class=\"Apple-style-span\"><B><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/B><\/SPAN><\/FONT><\/P><FONT class=\"Apple-style-span\" face=\"Verdana\" size=\"3\"><B><\/B><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! It seems that every single piece of software I install adds a stub application that starts automatically. I know software makers do not do it out of maliciousness, but good grief these people act as if theirs is the most important software on my computer. In the old days, it was pretty [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,87,26,3,45],"class_list":["post-54763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-processes","tag-registry","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! It seems that every single piece of software I install adds a stub application that starts automatically. I know software makers do not do it out of maliciousness, but good grief these people act as if theirs is the most important software on my computer. In the old days, it was pretty [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54763","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54763\/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=54763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}