{"id":52343,"date":"2009-10-01T03:01:00","date_gmt":"2009-10-01T03:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/10\/01\/hey-scripting-guy-how-can-i-use-windows-powershell-to-stop-services\/"},"modified":"2009-10-01T03:01:00","modified_gmt":"2009-10-01T03:01:00","slug":"hey-scripting-guy-how-can-i-use-windows-powershell-to-stop-services","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-windows-powershell-to-stop-services\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use Windows PowerShell to Stop Services?"},"content":{"rendered":"<p><!-- AddThis Button BEGIN --><a class=\"addthis_button\" href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;pub=scriptingguys\"><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><\/a><!-- AddThis Button END --><\/p>\n<p class=\"Readeraidonly\">(Portions of this blog post previously appeared in the Microsoft Press book, <a href=\"http:\/\/www.microsoft.com\/learning\/en\/us\/Book.aspx?ID=9541&amp;locale=en-us\">Windows PowerShell Scripting Guide<\/a>.)<\/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\"><\/p>\n<p class=\"MsoNormal\">Hey, Scripting Guy! I have some services that are running on my computer. I looked them up on TechNet and I do not believe I need them to be running. Is there a way I can write a Windows PowerShell script to stop the services? If I can, would you be able to write me a sample script to give me the idea of what I need to do? I sure would appreciate it. <\/p>\n<p>&lt;<\/p>\n<p>p style=&#8221;MARGIN: 0in 0in 8pt&#8221; class=&#8221;MsoNormal&#8221;&gt;&#8211; TM<\/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\"><\/p>\n<p class=\"MsoNormal\">Hello TM, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. One of my pet peeves is services that are running that I do not think need to be running. It seems like there are thousands of examples of these non-essential services running on my computer. The problem is that all of the services are used for something. One example is the Zune Bus Enumerator service. Why is that service running 24&#215;7 when I only connect my Zune to my computer once a month at the most? In my case, it is running because I am too lazy to stop and to restart the service. It needs to be running when I connect my Zune to my desktop, but most of the time it could just as well be turned off. This is just one such example. There are many other examples (maybe not thousands) that you will find on your computer. The trick is to know exactly what a service does and make an intelligent decision as to whether you can live without the benefits the service provides. In the past, I have written different scripts that put my computer into different configurations depending on which tasks I am planning on performing. One of these days, I will do that again. As with everything, make sure you test the configuration in a virtual machine before actually using it on a real computer. <\/p>\n<p class=\"MsoNormal\">TM, let&#8217;s take a look at stopping services using Windows PowerShell. There are two ways to stop services in Windows PowerShell. You can use the <b>Stop-Service<\/b> cmdlet, or you can use WMI. The two ways of stopping services are illustrated here using the bits service as an example:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Stop-Service &ndash;name bits<br>(Get-WmiObject -class win32_service -filter &#8220;name = &#8216;bits'&#8221;).stopService()<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">As you can see, the easiest way to stop a process is to use the <b>Stop-Service<\/b> cmdlet. In the StopService.ps1 script we stop the BITS service on our computer. There are two ways to stop a service using the <b>Stop-Service<\/b> cmdlet: by <b>name<\/b> or by <b>displayname<\/b>. In the StopService.ps1 script, we stop the BITS service by using its service name: bits. If we were to use the <b>displayname<\/b> property to stop the BITS service, we would need to type, &ldquo;Background Intelligent Transfer Service&rdquo;. In general, if you know the actual service name, it will be less typing to use the name rather than the <b>displayname<\/b> property to control the service. The <b>$strService<\/b> variable is used to hold the name of the service we wish to stop. After we have the name of the service, we use the <b>Stop-Service<\/b> cmdlet to stop the service. Here we use the <b>&ndash;name<\/b> parameter and supply it the name of the service to stop that is contained in the <b>$strService<\/b> variable. The StopService.ps1 script is seen here:<\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>StopService.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$strService = &#8220;bits&#8221;<br>Stop-Service -Name $strService<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"Readeraidonly\"><span>When using the <b>Stop-Service<\/b> cmdlet to stop a service, ensure that the script is running with administrator rights, or it will fail. The error shown in the following image will be generated if administrator rights are not utilized:<\/span><\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of error shown if script is run without administrator rights\" alt=\"Image of error shown if script is run without administrator rights\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/october\/hey1001\/hsg-10-01-09-01.jpg\" width=\"600\" height=\"171\"><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">If we wanted to stop several services, we could easily modify the StopService.ps1 script to accommodate our needs. The change to the script would entail creating an array of service names, and using the <b>foreach<\/b> statement to iterate through the array. The remainder of the script would stay essentially the same. <\/p>\n<p class=\"MsoNormal\">In the StopMultipleServices.ps1 script, we first create an array of service names. This is done in the first line of our script when we assign the name of several services to the variable <b>$aryServices<\/b>. We then use the <b>foreach<\/b> statement to iterate through the array of services. We use the <b>$strService<\/b> variable as the enumerator through the array. We then use the <b>Write-Host<\/b> cmdlet to display a message that says a particular service is being stopped. After we have done this, we call the <b>Stop-Service<\/b> cmdlet and pass it the name of the service to be stopped. The StopMultipleServices.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>StopMultipleServices.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$aryServices = &#8220;bits&#8221;, &#8220;wuauserv&#8221;, &#8220;CcmExec&#8221;<br>foreach ($strService in $aryServices)<br>{<br><span>&nbsp;<\/span>Write-Host &#8220;Stopping $strService &#8230;&#8221;<br><span>&nbsp;<\/span>Stop-Service -Name $strService<br>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\"><br>As mentioned earlier when we began the section on setting the service configuration, it makes sense to query the <b>acceptStop<\/b> property of the <b>Win32_Service<\/b> WMI class before attempting to stop the service. <\/p>\n<p class=\"Readeraidonly\">If you are having a problem with your script using the <b>Get-WmiObject<\/b> cmdlet and the <b>WIN32_Service<\/b> class, remember that the property names are not the same as the ones used by the <b>Get-Service<\/b> cmdlet. A good example of this is the fact that <b>Get-Service<\/b> uses <b>canstop<\/b> for the property that indicates if a service is stoppable. The <b>Get-WmiObject<\/b> cmdlet and <b>WIN32_Service<\/b> WMI class use the property <b>acceptStop<\/b> to indicate the exact same thing. <\/p>\n<p class=\"MsoNormal\">Not only does this make the script run faster and more efficiently, but it can also assist in preventing scripts from hanging. In the CheckServiceThenStop.ps1 script, we first create a variable called <b>$strService <\/b>that is used to hold the name of the service to stop. This can be hardcoded, or easily modified to accept command-line input. The easiest way to do that would be to modify <b>$strService<\/b> to use <b>$args<\/b>. The name of the computer that has the service we wish to stop is stored in the variable <b>$strComputer<\/b>. Here we are using the name <b>localhost<\/b> to refer to the local computer. We use the <b>$strClass<\/b> variable to hold the name of the WMI class to query: <b>WIN32_Service<\/b>, for this example. <\/p>\n<p class=\"MsoNormal\">We supply three arguments to the <b>Get-WmiObject<\/b> cmdlet: the <b>class<\/b>, the <b>computer<\/b>, and the <b>filter<\/b>. The <b>class<\/b> and <b>computer<\/b> arguments simply read the values stored in the <b>$strService<\/b> and <b>$strComputer<\/b> variables respectively. The <b>&ndash;filter<\/b> argument takes the place of a <b>where<\/b> clause from a WQL query. By using the <b>&ndash;filter<\/b> argument, the code is a bit cleaner than if we had to write the equivalent WQL query, which would look something like the following:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">&ldquo;Select * from win32_service where name = &lsquo;bits&rsquo;&rdquo;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">While the syntax is not horrid, it is a lot more typing than the Windows PowerShell statement. <\/p>\n<p class=\"Readeraidonly\">When using the <b>&ndash;filter<\/b> argument of the <b>Get-WMIObject<\/b> cmdlet, you do not need the word <b>where<\/b> in the filter. In fact, if you do include the word <b>where<\/b> in the filter, it will cause the filter to fail. So while it is the equivalent of WQL <b>where<\/b> clause, it does not include the word <b>where<\/b> in it. Remembering this will not only save you time typing, but also save you time troubleshooting as well. <\/p>\n<p class=\"MsoNormal\">We use the <b>if<\/b> statement to determine if we are going to attempt to stop the service or not. The nice thing is that because the <b>acceptStop<\/b> property is a Boolean (true\/false) value, we can simplify our syntax and use the <b>if(condition is true)<\/b> format. This is much better than typing something like the following:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">If ( $objWMIService.acceptStop &ndash;eq &ldquo;true&rdquo;) <\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Our code is exactly the same, but we save a little bit of typing and gain the benefit of more readable code in the process. The actual <b>if<\/b> statement is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">if( $objWMIService.Acceptstop )<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">We then open a set of curly brackets, use the <b>Write-Host<\/b> cmdlet to print out a message indicating our intention to attempt to stop the service specified in the <b>$strService<\/b> variable. After we have done this, we call the <b>stopService<\/b> method from the <b>WIN32_Service<\/b> class and attempt to stop the specified service. The variable <b>$rtn<\/b> is used to capture completion status information from the method call. A &ldquo;0&rdquo; indicates no errors; any other number would merit investigation. <\/p>\n<p class=\"MsoNormal\">To examine the return codes from the <b>stopService<\/b> method call, we use the <b>Switch<\/b> statement. If the <b>returnvalue<\/b> property is equal to 0, we use <b>Write-Host<\/b> to indicate that there were no errors, and the method completed successfully. Otherwise, we evaluate the error code for the more common errors, and display the appropriate message. If an error occurs that we did not anticipate, we use the <b>DEFAULT<\/b> switch to display the exact error number. The <b>Switch<\/b> statement is seen here:<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">Switch ($rtn.returnvalue) <br><span>&nbsp; <\/span>{ <br><span>&nbsp;&nbsp; <\/span>0 { Write-Host -foregroundcolor green &#8220;$strService stopped&#8221; }<br><span>&nbsp;&nbsp; <\/span>2 { Write-Host -foregroundcolor red &#8220;$strService service reports&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; access denied&#8221; }<br><span>&nbsp;&nbsp; <\/span>5 { Write-Host -ForegroundColor red &#8220;$strService service cannot&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; accept control at this time&#8221; }<br><span>&nbsp;&nbsp; <\/span>10 { Write-Host -ForegroundColor red &#8220;$strService service is already&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; stopped&#8221; }<br><span>&nbsp;&nbsp; <\/span>DEFAULT { Write-Host -ForegroundColor red &#8220;$strService service reports&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; ERROR $($rtn.returnValue)&#8221; }<br><span>&nbsp; <\/span>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">However, if the service will not accept a <b>stop<\/b> command, we use an <b>else<\/b> statement and use the <b>Write-Host<\/b> statement to display the name of the service and a statement to the effect that the service will not accept a stop request from the service controller. This should only occur if the service reports that it is not configured to accept a <b>stop<\/b> command. Keep in mind there could be the situation where the service is configured to accept a stop request, but it simply is not accepting a stop request at this time. This could occur when the service controller was busy working with another service. This should result in a return error code of 5, which is properly evaluated by the <b>Switch<\/b> statement. The completed CheckServiceThenStop.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>CheckServiceThenStop.ps1<\/strong><\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">$strService = &#8220;bits&#8221;<br>$strComputer = &#8220;localhost&#8221;<br>$strClass = &#8220;win32_service&#8221;<br>$objWmiService = Get-Wmiobject -Class $strClass -computer $strComputer `<br><span>&nbsp; <\/span>-filter &#8220;name = &#8216;$strService'&#8221;<br>if( $objWMIService.Acceptstop )<br><span>&nbsp;<\/span>{ <br><span>&nbsp; <\/span>Write-Host &#8220;stopping the $strService service now &#8230;&#8221; <br><span>&nbsp; <\/span>$rtn = $objWMIService.stopService()<br><span>&nbsp; <\/span>Switch ($rtn.returnvalue) <br><span>&nbsp; <\/span>{ <br><span>&nbsp;&nbsp; <\/span>0 { Write-Host -foregroundcolor green &#8220;$strService stopped&#8221; }<br><span>&nbsp;&nbsp; <\/span>2 { Write-Host -foregroundcolor red &#8220;$strService service reports&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; access denied&#8221; }<br><span>&nbsp;&nbsp; <\/span>5 { Write-Host -ForegroundColor red &#8220;$strService service cannot&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; accept control at this time&#8221; }<br><span>&nbsp;&nbsp; <\/span>10 { Write-Host -ForegroundColor red &#8220;$strService service is already&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; stopped&#8221; }<br><span>&nbsp;&nbsp; <\/span>DEFAULT { Write-Host -ForegroundColor red &#8220;$strService service reports&#8221; `<br><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8221; ERROR $($rtn.returnValue)&#8221; }<br><span>&nbsp; <\/span>}<br><span>&nbsp;<\/span>}<br>ELSE<br><span>&nbsp;<\/span>{ <br><span>&nbsp; <\/span>Write-Host &#8220;$strService will not accept a stop request&#8221;<br><span>&nbsp;<\/span>}<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Well, TM, that is about all there is to stopping services using Windows PowerShell. <\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be scripting tomorrow, follow us on <a href=\"http:\/\/www.twitter.com\/scriptingguys\/\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <a href=\"http:\/\/www.facebook.com\/group.php?gid=5901799452\" target=\"_blank\"><font face=\"Segoe\">Facebook<\/font><\/a>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p><b><font size=\"3\"><font face=\"Times New Roman\">Ed Wilson and Craig Liebendorfer, Scripting Guys<\/font><\/font><\/b><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Portions of this blog post previously appeared in the Microsoft Press book, Windows PowerShell Scripting Guide.) Hey, Scripting Guy! I have some services that are running on my computer. I looked them up on TechNet and I do not believe I need them to be running. Is there a way I can write a Windows [&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,3,39,45],"class_list":["post-52343","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scripting-guy","tag-services","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>(Portions of this blog post previously appeared in the Microsoft Press book, Windows PowerShell Scripting Guide.) Hey, Scripting Guy! I have some services that are running on my computer. I looked them up on TechNet and I do not believe I need them to be running. Is there a way I can write a Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52343","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=52343"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52343\/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=52343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}