{"id":5073,"date":"2012-08-27T00:01:00","date_gmt":"2012-08-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/08\/27\/three-cool-powershell-service-tricks\/"},"modified":"2012-08-27T00:01:00","modified_gmt":"2012-08-27T00:01:00","slug":"three-cool-powershell-service-tricks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/three-cool-powershell-service-tricks\/","title":{"rendered":"Three Cool PowerShell Service Tricks"},"content":{"rendered":"<p><b>Summary<\/b>: Ed Wilson, the Microsoft Scripting Guy, talks about three cool Windows PowerShell tricks for working with services.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It does not matter what ones role is, whether an Exchange Server admin, a Group Policy guru, or even an ordinary every day power user, one eventually needs to work with services. Today I will show three cool tricks that involve using Windows PowerShell to work with services.<\/p>\n<h2>Stop multiple services at once<\/h2>\n<p>One of the nice features of the <b>Get-Service<\/b> cmdlet is that it accepts an array. Therefore, I can find information about specific services by supplying the name of multiple services. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Service bits,wuauserv<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Status&nbsp;&nbsp; Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DisplayName<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&nbsp;&nbsp; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">Running&nbsp; bits&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Background Intelligent Transfer Ser&#8230;<\/p>\n<p style=\"padding-left: 30px\">Running&nbsp; wuauserv&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Windows Update<\/p>\n<p>The <b>Stop-Service<\/b> cmdlet accepts an array of ServiceController objects (the type of object returned by the <b>Get-Service <\/b>cmdlet). Therefore, I can pipe the results of the <b>Get-Service<\/b> cmdlet to the <b>Stop-Service<\/b> cmdlet to stop the array of services. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Service bits,wuauserv | Stop-Service<\/p>\n<p>To see what a command does prior to the actual execution of the command, use the <b>WhatIf<\/b><i> <\/i>switch. In the image that follows, the use of the <b>WhatIf<\/b><i> <\/i>switch to prototype the command appears, followed by the actual command to stop the services.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3036.HSG-8-27-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3036.HSG-8-27-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Start multiple services<\/h2>\n<p>The <b>Start-Service<\/b> cmdlet also accepts an array of ServiceController objects as pipelined input. This means that I can use <b>Get-Service<\/b> to look at my service status, and then pipe the objects to the <b>Start-Service<\/b> cmdlet if required. This command is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2626.HSG-8-27-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2626.HSG-8-27-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Stopping and disabling services in one command<\/h2>\n<p>A cool thing is that I can use the <b>Get-Service<\/b> cmdlet to check on a group of services. If I want to stop them, I can pipe the returned ServiceController object to the <b>Stop-Service<\/b> cmdlet. But what if I also do not want the services to start up on the next reboot? Here is where some really cool stuff comes into play&hellip;<\/p>\n<p>I use the <b>PassThru<\/b><i> <\/i>switch, and the ServiceController objects will pass along the pipeline. I can then use those objects with the <b>Set-Service<\/b> cmdlet and change the startup type of the services to Disabled. Here is the command to accomplish this task.<\/p>\n<p style=\"padding-left: 30px\">Get-Service bits,wuauserv | Stop-Service -PassThru | Set-Service -StartupType disabled<\/p>\n<p>Unfortunately, although <b>Set-Service<\/b> permits setting the startup type of the service to Disabled, the <b>Get-Service<\/b> cmdlet does not report the startup type of the service. To check on the results of our command, I need to use WMI. Here is the WMI command that I use to check the start mode of the services.<\/p>\n<p style=\"padding-left: 30px\">gwmi win32_service -filter &#8220;name = &#8216;bits&#8217; OR name = &#8216;wuauserv'&#8221;<\/p>\n<p>The command and the associate output from the command are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0447.HSG-8-27-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0447.HSG-8-27-12-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Join me tomorrow when I will talk about more way cool Windows PowerShell stuff.<\/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>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, the Microsoft Scripting Guy, talks about three cool Windows PowerShell tricks for working with services. Microsoft Scripting Guy, Ed Wilson, is here. It does not matter what ones role is, whether an Exchange Server admin, a Group Policy guru, or even an ordinary every day power user, one eventually needs to work [&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":[31,3,39,45],"class_list":["post-5073","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>Summary: Ed Wilson, the Microsoft Scripting Guy, talks about three cool Windows PowerShell tricks for working with services. Microsoft Scripting Guy, Ed Wilson, is here. It does not matter what ones role is, whether an Exchange Server admin, a Group Policy guru, or even an ordinary every day power user, one eventually needs to work [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5073","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=5073"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/5073\/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=5073"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=5073"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=5073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}