{"id":11481,"date":"2012-01-14T00:01:00","date_gmt":"2012-01-14T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/01\/14\/use-powershell-to-group-and-format-output\/"},"modified":"2012-01-14T00:01:00","modified_gmt":"2012-01-14T00:01:00","slug":"use-powershell-to-group-and-format-output","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-group-and-format-output\/","title":{"rendered":"Use PowerShell to Group and Format Output"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, teaches how to use Windows PowerShell to group and to format output.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that it allows you to work the way that you like to do so. The other day, I was making a presentation to the <a href=\"http:\/\/powershellgroup.org\/charlotte.nc\" target=\"_blank\">Charlotte PowerShell Users Group<\/a>. The photo that follows shows me talking, and the Scripting Wife and Microsoft PFE Jason Walker at this first ever meeting.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5732.wes-1-14-12-1.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5732.wes-1-14-12-1.jpg\" alt=\"Photo\" title=\"Photo\" \/><\/a><\/p>\n<p>One of the attendees asked, &ldquo;Is Windows PowerShell a developer technology or a network administrator type of technology?&rdquo; Before I could even answer the question, someone else jumped in and said that Windows PowerShell is really powerful, and that it has a number of things that would appeal to developers. However, he continued, the main thing about Windows PowerShell is that it allows you to process large amounts of data very quickly. Cool, I thought to myself; I did not see the need to add anything else to the conversation.<\/p>\n<p>One of the fundamental aspects of working with data is grouping the data to enable viewing relationships in a more meaningful way. Earlier this week, I looked at <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/01\/11\/use-the-powershell-group-object-cmdlet-to-display-data.aspx\" target=\"_blank\">using the Group-Object cmdlet<\/a> to group information.<\/p>\n<p>The <b>Group-Object<\/b> cmdlet does a good job of grouping Windows PowerShell objects for display, but there are times when a simple grouping might be useful in a table. The problem is that the syntax is not exactly intuitive. For example, it would seem that the command that is shown here would work.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | Format-Table name, status -GroupBy status<\/p>\n<p>When the command runs, however, the output (shown in the following image) appears somewhat jumbled.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1374.HSG-01-14-12-02.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1374.HSG-01-14-12-02.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In fact, the first time I ran across this, the output confused me because it looks like it is grouping the output. The second time I ran across this grouping behavior, the output seriously disappointed me because I realized that it was not really grouping the output. Then it dawned on me, I need to sort the output prior to sending it to the <b>Format-Table<\/b> command. I therefore, modified the command to incorporate the <b>Sort-Object <\/b>cmdlet. The revised command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | Sort-Object status | Format-Table name, status -GroupBy status<\/p>\n<p>After it is sorted by the <b>Status<\/b> property, the service information displays correctly in the grouped table. This revised output is shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7043.HSG-01-14-12-03.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7043.HSG-01-14-12-03.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>As might be expected, this non-grouping behavior also exists with the <b>Format-List<\/b> cmdlet, which is a cmdlet that also contains the <i>GroupBy <\/i>parameter. The code that follows appears to group the output, until one takes a closer look at the output.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | Format-List name, status -GroupBy status<\/p>\n<p>A look at the output (shown in the following image) shows that the grouping occurs only when concurrent services share the same status.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3617.HSG-01-14-12-04.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3617.HSG-01-14-12-04.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>The fix for the grouped output from the <b>Format-List <\/b>cmdlet is the same as the fix for the <b>Format-Table<\/b> cmdlet&mdash;first sort the output by using the <b>Sort-Object <\/b>cmdlet, then pipe the sorted service objects to the <b>Format-List <\/b>cmdlet for grouping. The revised code is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | sort-object status | Format-List name, status -GroupBy status<\/p>\n<p>The revised command and the associated sorted output from the command are shown in the image that follows.<\/p>\n<p><b><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1184.HSG-01-14-12-05.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1184.HSG-01-14-12-05.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><br \/><\/b><\/p>\n<p>One of the cool things to do with the <b>Format-List <\/b>cmdlet is to use a <b>ScriptBlock<\/b> in the <i>GroupBy <\/i>parameter. Once again, it is necessary to sort the output prior to sending it to the <b>Format-List <\/b>cmdlet. In fact, you may need to sort on more than one parameter, as illustrated in the code that follows. (This code is a single line command that is broken at the pipe character for readability).<\/p>\n<p style=\"padding-left: 30px\">Get-Service | sort-object status, canstop |<\/p>\n<p style=\"padding-left: 30px\">Format-List name, status &ndash;GroupBy {$_.status -eq &#8216;running&#8217; -AND $_.canstop}<\/p>\n<p>To make the output easier to assess, I added the <i>Unique <\/i>switched parameter to the <b>Sort-Object <\/b>cmdlet to shorten the output. Interestingly enough, the first condition reports two services for the first condition. This is because each -AND combination equals False.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4274.HSG-01-14-12-06.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4274.HSG-01-14-12-06.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p><b>Format-Table<\/b> also accepts a <b>ScriptBlock<\/b> for the <i>GroupBy <\/i>parameter. It works the same way that the <b>Format-List <\/b>behaves. The code that follows creates two tables, one that evaluates to False, and one that evaluates to True.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | sort-object status, canstop -unique |<\/p>\n<p style=\"padding-left: 30px\">Format-table name, canstop, status -GroupBy {$_.status -eq &#8216;running&#8217; -AND $_.canstop}<\/p>\n<p>The image that follows illustrates creating a table that groups output based on a <b>ScriptBlock<\/b><i>.<\/i><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2541.HSG-01-14-12-07.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2541.HSG-01-14-12-07.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Well, that is about all there is to grouping output information by using the <b>Format-Table<\/b> and the <b>Format-List<\/b> cmdlets.<\/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: Microsoft Scripting Guy, Ed Wilson, teaches how to use Windows PowerShell to group and to format output. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that it allows you to work the way that you like to do so. The other day, I was making a [&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,61,45],"class_list":["post-11481","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, teaches how to use Windows PowerShell to group and to format output. Microsoft Scripting Guy, Ed Wilson, is here. One of the cool things about Windows PowerShell is that it allows you to work the way that you like to do so. The other day, I was making a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11481","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=11481"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11481\/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=11481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}