{"id":9541,"date":"2012-05-03T00:01:00","date_gmt":"2012-05-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/05\/03\/display-easily-consumed-service-information-in-powershell\/"},"modified":"2012-05-03T00:01:00","modified_gmt":"2012-05-03T00:01:00","slug":"display-easily-consumed-service-information-in-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/display-easily-consumed-service-information-in-powershell\/","title":{"rendered":"Display Easily Consumed Service Information in PowerShell"},"content":{"rendered":"<p><b>Summary<\/b>: Learn how to display easily consumed information about system services by using Windows PowerShell.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! I am still having trouble working with the <b>Out-GridView<\/b> cmdlet. I sort of understood what you were talking about yesterday, but when I tried it, all I got was errors. What does it mean when it says that it does not accept the input?<\/p>\n<p>&mdash;RS<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello RS,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today the Scripting Wife and I are heading back from the Mark Minasi Conference. We do not have a lot of time to tarry because tonight the Charlotte PowerShell Users Group has their meeting. We have a really cool presentation by Brian Hitney. We are going to attempt to do a live meeting so we can share the experience with our fellow Windows PowerShell friends.<\/p>\n<p>RS, one thing to keep in mind when you use the <b>Out-GridView<\/b> cmdlet is that you need to send it usable information. This means staying away from Format* cmdlets such as <b>Format-Table<\/b>, <b>Format-List<\/b>, or <b>Format-Wide<\/b>. The reason for not using the Format* cmdlets is they change the object; therefore, the <b>Out-GridView<\/b> cmdlet does not know what to do with the incoming properties.<\/p>\n<p>To provide the appropriate filtering information, the <b>Out-GridView<\/b> cmdlet reads the incoming property types. If those incoming property types change (which is the case when you pipe through a Format* type of cmdlet), <b>Out-GridView<\/b> does not know how to display the information. The following example illustrates this mutable property type situation. The results of the <b>Get-Service<\/b> cmdlet returns a <b>ServiceController<\/b> object. When the <b>ServiceController<\/b> object pipes to the <b>Format-Table<\/b> cmdlet, a series of formatting specific objects return instead.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | Format-Table name, status | Get-Member<\/p>\n<p>The command and its associated output appear in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6283.hsg-5-3-12-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6283.hsg-5-3-12-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The <b>Out-GridView<\/b> cmdlet does not know what to do with all the different Format* types of objects. If you need to select specific properties to display in the <b>Out-GridView<\/b> cmdlet, use the <b>Select-Object<\/b> cmdlet instead of a Format* type of cmdlet (such as <b>Format-Table<\/b>). In fact, the use of <b>Format-Table<\/b> and <b>Select-Object<\/b> are extremely similar. It is common to use <b>Select-Object<\/b> prior to sending data to <b>Format-Table&mdash;<\/b>especially if you want to limit the number of objects that are returned by a query. For example, the following command chooses the name and status properties from <b>ServiceController<\/b> objects, but it only returns the first five services.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Service | sort name -Descending | Select-Object -First 5 -Property name,<\/p>\n<p style=\"padding-left: 30px\">status | Format-Table<\/p>\n<p>If you leave the Format-Table cmdlet from the end of the command, you have the following command.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Service | sort name -Descending | Select-Object -First 5 -Property name,<\/p>\n<p style=\"padding-left: 30px\">Status<\/p>\n<p>If you run both commands and compare the output, you will see the output is amazingly similar. This is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3678.hsg-5-3-12-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3678.hsg-5-3-12-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In general, you can leave the <b>Format-Table<\/b> cmdlet out of such commands. This makes it easier for you to further process the information, such as sending the output to the <b>Out-GridView<\/b> cmdlet. For example, RS, to go back to your original problem, sending the first command (with the <b>Format-Table<\/b>) cmdlet in the command to the <b>Out-GridView<\/b> cmdlet produces an error. The error, a <b>FormatException<\/b>, is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2526.hsg-5-3-12-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2526.hsg-5-3-12-03.png\" alt=\"Image of error message\" title=\"Image of error message\" \/><\/a><\/p>\n<p>Dropping the <b>Format-Table<\/b> cmdlet from the end of the command permits the command to work properly. The revised command is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Service | sort name -Descending | Select-Object -First 5 -Property name,<\/p>\n<p style=\"padding-left: 30px\">status | Out-GridView<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt;<\/p>\n<p>The <b>GridView<\/b> control that is produced by the previous command is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8424.hsg-5-3-12-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8424.hsg-5-3-12-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One thing to keep in mind, is that the <b>Out-GridView<\/b> cmdlet accepts the format of the input object when creating the <b>GridView<\/b> control. This means that because the default view from <b>Get-Service<\/b> only displays three columns (the name, status, and display name), these are the only properties directly available to the <b>GridView<\/b> control if you pipe the output without any further manipulation.<\/p>\n<p>The easy way to ensure the availability of all properties involves using the <b>Select-Object<\/b> cmdlet to pick up the additional properties, as shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | Select-Object -Property * | Out-GridView<\/p>\n<p>If you do very much preprocessing prior to sending your data to the <b>Out-GridView<\/b> cmdlet, you may want to create a custom title for the <b>GridView <\/b>control. The reason for creating a custom title is that by default, the title is the query that creates the control. But if your query is very involved, complex, or confusing, the title will mimic this aspect of your code. A better way is to create a custom title that more directly answers the question, &ldquo;Why did I create this control?&rdquo; This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Service | Select-Object -Property * | Out-GridView -Title &#8220;All Service Properties&#8221;<\/p>\n<p>The <b>GridView<\/b> control that displays all the service properties and has a custom title of <i>All Service Properties <\/i>is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6837.hsg-5-3-12-05.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6837.hsg-5-3-12-05.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Although it is certainly possible to use C<i>riteria <\/i>to filter out running from stopped services when you have produced the <b>GridView<\/b> control, if you know you only want running services, filter prior to sending the output to the <b>Out-GridView<\/b> cmdlet. This will make a cleaner way to deal with your data. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Service | where { $_.status -eq &#8216;running&#8217;} | select * | Out-GridView -Title &#8216;running services&#8217;<\/p>\n<p>The custom <b>GridView<\/b> control that displays all the properties of running services with the title of <i>running services <\/i>is shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4073.hsg-5-3-12-06.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4073.hsg-5-3-12-06.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>RS, that is all there is to using the <b>Out-GridView<\/b> cmdlet to display service information. Join me tomorrow when I will spend a bit more time talking about the <b>Out-GridView<\/b> cmdlet.<\/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: Learn how to display easily consumed information about system services by using Windows PowerShell. &nbsp;Hey, Scripting Guy! I am still having trouble working with the Out-GridView cmdlet. I sort of understood what you were talking about yesterday, but when I tried it, all I got was errors. What does it mean when it says [&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":[51,3,4,45],"class_list":["post-9541","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to display easily consumed information about system services by using Windows PowerShell. &nbsp;Hey, Scripting Guy! I am still having trouble working with the Out-GridView cmdlet. I sort of understood what you were talking about yesterday, but when I tried it, all I got was errors. What does it mean when it says [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9541","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=9541"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/9541\/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=9541"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=9541"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=9541"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}