{"id":52373,"date":"2009-09-28T03:01:00","date_gmt":"2009-09-28T03:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/09\/28\/hey-scripting-guy-can-i-use-windows-powershell-to-find-the-status-of-services-on-my-computer\/"},"modified":"2009-09-28T03:01:00","modified_gmt":"2009-09-28T03:01:00","slug":"hey-scripting-guy-can-i-use-windows-powershell-to-find-the-status-of-services-on-my-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-can-i-use-windows-powershell-to-find-the-status-of-services-on-my-computer\/","title":{"rendered":"Hey, Scripting Guy! Can I Use Windows PowerShell to Find the Status of Services on My Computer?"},"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=\"ListReaderaidonly\">(Portions of this article previously appeared in the Microsoft Press book, <a href=\"http:\/\/www.microsoft.com\/learning\/en\/us\/Book.aspx?ID=9541&amp;locale=en-us\"><font face=\"Segoe\">Windows PowerShell Scripting Guide<\/font><\/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 would like to use Windows PowerShell to obtain information about the status of services that are configured on my computer. Do you have any hints you could share?<\/p>\n<p class=\"MsoNormal\">&#8212; LB<\/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 LB, <\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. This is like <a title=\"deja vu all over again\" href=\"http:\/\/en.wikipedia.org\/wiki\/Yogi_Berra#Quotes\"><font face=\"Segoe\">deja vu all over again<\/font><\/a>. I have received nearly a dozen e-mail messages this week to the <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> e-mail address that were all related to working with services on a computer. As a result of this mail, I decided I would gather some of the questions together and look at services this week. If you have some questions during the week, you can ping us on <a href=\"https:\/\/twitter.com\/scriptingguys\/\">Twitter<\/a> or write something on my wall on <a href=\"http:\/\/www.facebook.com\/Mr.Ed.Wilson\"><font face=\"Segoe\">Facebook<\/font><\/a>.<span>&nbsp; <\/span>Of course, you are also free to send comments to <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> as well. <\/p>\n<p class=\"MsoNormal\">From both a performance perspective and a security perspective, it is important to know which services are actually running on a server or workstation. To gather this information, there are two cmdlets that can perform the task. The first is the <b>Get-Service<\/b> cmdlet, and the second is the <b>Get-WmiObject<\/b> cmdlet. <\/p>\n<p class=\"MsoNormal\">From a functionality standpoint, the <b><span>&nbsp;<\/span>Get-WmiObject<\/b> cmdlet will use the <b>WIN32_service<\/b> class and has more capabilities such as the ability to change the configuration of a service. The added functionality comes with a price<span>&mdash;<\/span>it is a bit more difficult to use.<span>&nbsp; <\/span><\/p>\n<p class=\"MsoNormal\">When using the <b>Get-Service<\/b> cmdlet, the default behavior is to return a listing of all the services on the computer. The output lists all services, both running and stopped. There are only three properties returned: <b>status<\/b>, <b>name<\/b>, and <b>displayname<\/b>. The list is alphabetized by service name. The default output from <b><span>&nbsp;<\/span>Get-Service<\/b> is seen here (in truncated form):<\/p>\n<p class=\"CodeBlock\"><span><font face=\"Lucida Sans Typewriter\">PS C:&gt; Get-Service<\/p>\n<p>Status<span>&nbsp;&nbsp; <\/span>Name<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>DisplayName<br>&#8212;&#8212;<span>&nbsp;&nbsp; <\/span>&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;&#8212;&#8212;&#8211;<br>Running<span>&nbsp; <\/span>AeLookupSvc<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Application Experience<br>Stopped<span>&nbsp; <\/span>ALG<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Application Layer Gateway Service<br>Running<span>&nbsp; <\/span>Appinfo<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Application Information<br>Stopped<span>&nbsp; <\/span>AppMgmt<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Application Management<br>Running<span>&nbsp; <\/span>AudioEndpointBu&#8230; Windows Audio Endpoint Builder<br>Running<span>&nbsp; <\/span>Audiosrv<span>&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>Windows Audio<br>Running<span>&nbsp; <\/span>BFE<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Base Filtering Engine<br>Running<span>&nbsp; <\/span>BITS<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>Background Intelligent Transfer Ser&#8230;<\/p>\n<p><\/font><\/span><\/p>\n<p class=\"MsoNormal\">If you want to know how many services are defined on the machine, you can use the CountServices.ps1 script. On my Windows Vista machine there are 139 services registered; clearly this becomes a management issue. Knowing the number of services registered on a machine may be useful as a simple indicator of static state on a machine. It is, of course, not a total indicator because you could uninstall a service and install a different one, and you would still have 139 services. But as a quick indicator it is useful. In the CountServices.ps1 script, we use the <b>Get-Service<\/b> cmdlet, surround the cmdlet name with parentheses, and then query the <b>length<\/b> property. The <b>length<\/b> property is used to count the number of services on the machine. The parentheses are used to tell Windows PowerShell to execute the code inside the parentheses first, and then perform the action on the outside of the parentheses&mdash;in this case, perform a count. The one-line CountServices.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>CountServices.ps1<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">(Get-Service).length<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Perhaps you are interested in services that are running. At this point, we can still use the <b>Get-Service<\/b> cmdlet, but we will need to bring in some additional power in the form of the <b>Where-Object<\/b>. To get the running services, we need to first use the <b>Get-Service<\/b> cmdlet to retrieve a listing of all the services. We then pipe the resulting object into the <b>Where-Object<\/b> cmdlet. After we are in the <b>Where-Object<\/b>, we use a script block to examine the status of each service in the object. We reference the current object by using the <b>$_<\/b> automatic variable. We use the <b>&ndash;eq<\/b> operator to see if the status is equal to the word running. If it is, we receive it into the new <b>psobject<\/b> that gets created as a result of the <b>Where-Object<\/b>. We then surround the entire code on both sides of the pipeline object and query the <b>length<\/b> property with parentheses. This is what is displayed. The parentheses are used to force execution of the inside code before obtaining the length. The CountRunningServices.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>CountRunningServices.ps1<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">(Get-Service | <br>where-object { $_.status -eq &#8220;running&#8221; }).length<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Having the count of running services is very good. As with the count of installed services, it provides a quick &ldquo;sanity check&rdquo; to let us know if something has changed. This makes it easier to manage than just glancing at the services.msc management console, which is seen in the following image.<span>&nbsp; <\/span>The management console interface is a bit busy, and as a result is not a good tool to use for a fast overview of service status. Windows PowerShell can provide a more comprehensive overview of the service situation on a computer. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of services.msc\" alt=\"Image of services.msc\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0928\/hsg-09-28-09-01.jpg\" width=\"600\" height=\"429\"><\/p>\n<p class=\"MsoNormal\">&nbsp;<\/p>\n<p class=\"MsoNormal\">Of course, the same caveat applies to using the count of defined services. It most certainly is not a substitute for auditing, and it does not provide enhanced security. It is just a quick &ldquo;visual indicator&rdquo; to tell if something has changed.<span>&nbsp; <\/span><\/p>\n<p class=\"MsoNormal\">If checking the number of running services is useful from a day-to-day management scenario, documenting the names of the running services is even more important. There are several reasons for writing the service information to a text file: <\/p>\n<p class=\"MsoNormal\"><span><span>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span>It is an easy process and provides a convenient means to check the state of the server. <\/p>\n<p class=\"MsoNormal\"><span><span>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span>It is useful from a documentation standpoint. <\/p>\n<p class=\"MsoNormal\"><span><span>&middot;<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span><\/span><\/span>To maintain baseline configuration. <\/p>\n<p class=\"MsoNormal\">As an example, suppose you want to optimize your server by turning off all unnecessary services. It would make sense to write out the existing configuration before beginning to make extensive changes that could lead to a disaster. By having a documented working configuration, you can simplify this task. Writing running services to a text file is illustrated in the <span>WriteRunningServicesToTxt.ps1 script. The text file created by the WriteRunningServicesToTxt.ps1 script is seen in <\/span>the following image<span>. The automatic column headers make the file easy to read, but could present a problem when used as input, unless certain allowances are made such as skipping the header line and the separator line.<\/p>\n<p><\/span><\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of text file created by running WriteRunningServicesToTxt.ps1\" alt=\"Image of text file created by running WriteRunningServicesToTxt.ps1\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0928\/hsg-09-28-09-02.jpg\" width=\"600\" height=\"432\"><\/p>\n<p class=\"Fig-Graphic\">&nbsp;<\/p>\n<p class=\"MsoNormal\"><span>In the WriteRunningServicesToTxt.ps1 script, we first create a variable called <b>$strState<\/b> and assign the string running to it. We then create a variable called <b>$strPath<\/b> and assign a path on our local system to it. In this example, we use the path, c:fsomyservice.txt. Note that we include the prospective file name in the path. We then use the <b>Get-Service<\/b> cmdlet and pipeline the resulting objects to the <b>Where-Object<\/b> cmdlet. The <b><span>&nbsp;<\/span>Where-Object<\/b> cmdlet is used as a filter to weed out every service that is not running. After we have filtered the objects, we pipe the results to the <b>Out-File<\/b> cmdlet and feed the string contained in the <b>$strPath<\/b> variable to the <b>&ndash;filepath<\/b> parameter. The completed WriteRunningServicesToTxt.ps1 script is seen here.<\/p>\n<p><\/span><\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>WriteRunningServicesToTxt.ps1<\/p>\n<p><\/strong><\/p>\n<p><font size=\"1\"><font><font face=\"Lucida Sans Typewriter\"><\/p>\n<p>&lt;<\/p>\n<p>p style=&#8221;MARGIN: 15pt 17.3pt 0.25in 0in&#8221; class=&#8221;CodeBlockScreened&#8221;&gt;<font size=\"2\">$strState = &#8220;running&#8221;<br>$strPath = &#8220;c:fsomyservice.txt&#8221;<br>Get-Service |<br>Where-Object { $_.status -eq $strState } | <br>Out-File -FilePath $strPath<\/p>\n<p><\/font><\/p>\n<p><\/font><\/font><\/font><\/p>\n<p class=\"MsoNormal\">In addition to simply writing service information to a text file, we can also write the results to a .csv file. This actually becomes a useful solution that opens many exciting doors. For example, Microsoft Excel and Microsoft Access like csv files, and Microsoft Word can take a .csv file and easily turn it into a table. But that is just Microsoft Office products; Microsoft SQL server loves .csv files nearly as much as Microsoft Excel does. The one thing I would recommend is that you visualize what your destination application is going to look like, and use PowerShell to configure the .csv file into an acceptable format for the final consumer of the data. For example, if your worksheet is a simple two columns with the first column being a service name and the second column being the status, it makes sense to clean up your .csv file to eliminate all the unnecessary properties before importing it into Microsoft Excel. This is what we do in the ExportRunningServices.ps1 script: We eliminate all properties except <b>name<\/b>, <b>startmode<\/b>, and <b>startname<\/b> before exporting the data to the .csv file. The resulting output is seen here:<\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Image of resulting output of running script\" alt=\"Image of resulting output of running script\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0928\/hsg-09-28-09-03.jpg\" width=\"600\" height=\"429\"><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/september\/hey0928\/hsg-09-28-09-03.jpg\"><span><font face=\"Segoe\"><\/font><\/span><\/a><\/p>\n<p class=\"MsoNormal\">In the ExportRunningServices.ps1 script, we first create a variable called <b>$strState<\/b> and we assign the string running to it. We then create a variable called <b>$strPath<\/b> that will hold the string representing the path to our exported file. We then use the <b>Get-WmiObject<\/b> cmdlet to retrieve the <b>WIN32_Service<\/b> WMI class. We supply the string contained in the <b>$strState<\/b> variable to the <b>&ndash;filter<\/b> parameter of the <b>Get-WmiObject<\/b> cmdlet. We then pipe the resulting object to the Select-Object cmdlet where we choose the <b>name<\/b>, <b>startmode<\/b>, and <b>startname<\/b> properties from the <b>WIN32_Service<\/b> WMI class. We then export the object to a .csv file by using the <b>Export-Csv<\/b> cmdlet while supplying the string contained in the <b>$strPath<\/b> variable to the <b>&ndash;path<\/b> parameter. The ExportRunningServices.ps1 script is seen here. <\/p>\n<p class=\"CodeBlockScreenedHead\"><strong>ExportRunningServices.ps1<\/p>\n<p><\/strong><\/p>\n<p class=\"CodeBlockScreened\"><span><font><font face=\"Lucida Sans Typewriter\">$strState = &#8220;running&#8221;<br>$strPath = &#8220;C:FSOservice.csv&#8221;<br>Get-WmiObject win32_service -Filter &#8220;state=&#8217;$strState'&#8221; |<br>select-object name, startmode, startname | <br>Export-Csv -Path $strPath<\/p>\n<p><\/font><\/font><\/span><\/p>\n<p class=\"MsoNormal\">Well, LB, that is about it for documenting the status of services. Join us tomorrow as Services Week continues. <\/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\">Twitter<\/a> or <a href=\"http:\/\/www.new.facebook.com\/group.php?gid=5901799452&amp;ref=ts\" 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><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p class=\"CodeBlockScreenedHead\">&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(Portions of this article previously appeared in the Microsoft Press book, Windows PowerShell Scripting Guide.) Hey, Scripting Guy! I would like to use Windows PowerShell to obtain information about the status of services that are configured on my computer. Do you have any hints you could share? &#8212; LB Hello LB, Microsoft Scripting Guy Ed [&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-52373","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 article previously appeared in the Microsoft Press book, Windows PowerShell Scripting Guide.) Hey, Scripting Guy! I would like to use Windows PowerShell to obtain information about the status of services that are configured on my computer. Do you have any hints you could share? &#8212; LB Hello LB, Microsoft Scripting Guy Ed [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52373","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=52373"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/52373\/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=52373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=52373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=52373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}