{"id":7611,"date":"2015-02-26T00:01:00","date_gmt":"2015-02-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/02\/26\/use-wmi-and-powershell-to-work-with-services\/"},"modified":"2019-02-18T10:30:29","modified_gmt":"2019-02-18T17:30:29","slug":"use-wmi-and-powershell-to-work-with-services","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-wmi-and-powershell-to-work-with-services\/","title":{"rendered":"Use WMI and PowerShell to Work with Services"},"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 and WMI to work with services.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. It is that time of the year. I just spent the last couple of hours uninstalling useless software from my laptop. This includes stuff that I intentionally installed and junk that becomes installed with other programs. I also spent a decent amount of time reviewing start-up programs and services. This is pretty much a stop gap measure &ndash; it is only a matter of time before I FDISK my machine and perform a complete reinstall.<\/p>\n<p>It still seems necessary to reinstall every year, which is a lot better than it used to be. I can remember a time when I reinstalled twice a year. The Scripting Wife has been talking about reinstalling her laptop for a while. I was kind of hoping to avoid doing all that until Windows 10 ships, but now I am not so sure.<\/p>\n<p>The problem is that although reinstallation is pretty easy these days, finding all the keys, original disks, and so on that seems to take forever. In my case, it seems that I continually find stuff that I am missing for weeks.<\/p>\n<p>I am in the middle of a project now, so I want to avoid that huge time hit for as long as I can. But I did feel the need to do some maintenance and optimization. Luckily, I have Windows PowerShell to aid in the task.<\/p>\n<h2>Using native Windows PowerShell commands for services<\/h2>\n<p>Windows PowerShell has always had the <b>Get-Service<\/b> cmdlet. It is great for what it does&mdash;and that is to show basic service information. By default, it has an output such as the following:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>As shown here, I can easily sort the output by piping it to the <b>Sort-Object<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">Get-Service | sort status &ndash;Descending<\/p>\n<p>To see the running processes first, I need to use the <b>&ndash;Descending<\/b> switch because <b>R<\/b> (for running) comes before <b>S (<\/b>for stopped).<\/p>\n<p>I can also get an overview of how many started and stopped services I have by using the <b>Group-Object<\/b> cmdlet. This technique is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Service | sort status -Descending | group status -NoElement<\/p>\n<p style=\"margin-left:30px\">Count Name<\/p>\n<p style=\"margin-left:30px\">&#8212;&#8211; &#8212;-<\/p>\n<p style=\"margin-left:30px\">&nbsp; 101 Running<\/p>\n<p style=\"margin-left:30px\">&nbsp; 109 Stopped<\/p>\n<p>This is all based on the default output. What information is available via the <b>Get-Service<\/b> cmdlet? The best way to find out is to select a single service and send the output to the <b>Format-List<\/b> cmdlet. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>Using WMI<\/h2>\n<p>If I want more information about the services, I need to use WMI. Here is the default output when I use the <b>Get-CimInstance Win32_Service<\/b> command:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>To get a better look at what is available, I use the <b>&ndash;Filter<\/b> parameter to return only information about the Bits service:<\/p>\n<p style=\"margin-left:30px\">Get-CimInstance Win32_Service -Filter &quot;name = &#039;bits&#039;&quot; | fl *<\/p>\n<p>The output is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>For the purposes of optimizing my laptop, I like the following properties:<\/p>\n<ul>\n<li>Description<\/li>\n<li>StartName<\/li>\n<li>StartMode<\/li>\n<li>PathName<\/li>\n<\/ul>\n<p>This information tells me what the service does, what account it uses to start up, how the service starts, and even the executable it uses. This information helps me make the decisions I need. Lately, I have found that when I search for information about services on the Internet, I get pages of spam sites that are useless (at best).<\/p>\n<p>This is why I am glad that I can get this kind of detailed information via Windows PowerShell. When I need additional information, I search TechNet or MSDN directly, and I can normally find good stuff there.<\/p>\n<p>I use the following command to obtain the output from WMI:<\/p>\n<p style=\"margin-left:30px\">$p = &#039;name&#039;, &#039;startname&#039;, &#039;startmode&#039;, &#039;pathname&#039;, &#039;description&#039;<\/p>\n<p style=\"margin-left:30px\">Get-CimInstance Win32_Service -Filter &quot;state = &#039;running&#039;&quot; -Property $P | FL $P<\/p>\n<p>The command and its output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-2-26-15-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to u<span>sing WMI and Windows PowerShell to work with services.&nbsp;<\/span>WMI Week will continue tomorrow when I will talk about more cool 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><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to work with services. Microsoft Scripting Guy, Ed Wilson, is here. It is that time of the year. I just spent the last couple of hours uninstalling useless software from my laptop. This includes stuff that I intentionally installed and junk that [&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":[3,4,45,6],"class_list":["post-7611","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","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 and WMI to work with services. Microsoft Scripting Guy, Ed Wilson, is here. It is that time of the year. I just spent the last couple of hours uninstalling useless software from my laptop. This includes stuff that I intentionally installed and junk that [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7611","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=7611"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7611\/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=7611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}