{"id":11511,"date":"2012-01-11T00:01:00","date_gmt":"2012-01-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/01\/11\/use-the-powershell-group-object-cmdlet-to-display-data\/"},"modified":"2012-01-11T00:01:00","modified_gmt":"2012-01-11T00:01:00","slug":"use-the-powershell-group-object-cmdlet-to-display-data","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-the-powershell-group-object-cmdlet-to-display-data\/","title":{"rendered":"Use the PowerShell Group-Object Cmdlet to Display Data"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, teaches how to use the Windows PowerShell <b>Group-Object<\/b> cmdlet to organize data.<\/p>\n<p><span><span><span><span><span><span><span><span><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\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hey, Scripting Guy! I have been using Windows PowerShell more these days. I find it really easy to use, and I like the way I can find things. But what I need is a better way to view things. For example, I have been saving stuff as a CSV file, and then opening the data in Microsoft Excel. This works OK, but I would like to be able to avoid the middleman so to speak. In other words, I want to be able to group information so it is easier to read. Can you think of a shortcut for grouping information?<\/p>\n<p>&mdash;MS<\/p>\n<p><span><span><span><span><span><span><span><span><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\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>&nbsp;Hello MS,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. MS, I am glad that you are using Windows PowerShell more on a daily basis. In fact, learning Windows PowerShell makes a great <a href=\"http:\/\/redmondmag.com\/articles\/2012\/01\/01\/12-new-years-resolutions-for-2012.aspx?sc_lang=en\" target=\"_blank\">New Year&rsquo;s resolution<\/a>, which is something it seems that some people are actually doing, based on tweets coming across on Twitter. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/01\/28\/why-should-i-learn-powershell-real-world-example-saves-the-day.aspx\">On January 28<\/a>, Tim Bolton writes a guest blog that talks about why it is good to learn Windows PowerShell, and in his initial email to me, he stated that this was something he wrestled with for quite some time.<\/p>\n<p>One of the things about learning to use Windows PowerShell is that Windows PowerShell can slice-and-dice<i> <\/i>data so easily. It becomes a quick data analysis tool that allows network administrators, analysts, and others to parse data to quickly discover and remediate issues. It can also be used to audit baseline information, or even to spelunk through reams of statistical data.<\/p>\n<p>One cmdlet that allows this analysis is the <b>Group-Object <\/b>cmdlet. In its most basic form, the <b>Group-Object <\/b>cmdlet accepts a property from objects in a pipeline, and it gathers up groups that match that property and displays the results. For example, to check on the status of services on a system, pipe the results from the <b>Get-Service <\/b>cmdlet to the <b>Group-Object <\/b>cmdlet and use the <b>Status<\/b><i> <\/i>property. The command is shown here.<\/p>\n<p style=\"padding-left: 30px\">Get-Service | Group-Object -Property status<\/p>\n<p>The command to group services based on the status of the service, along with its resultant output is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4064.HSG-01-11-12-01.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4064.HSG-01-11-12-01.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>In the <b>Group<\/b><i> <\/i>field of the output from the <b>Group-Object <\/b>cmdlet, the objects that are grouped appear. The output indicates that each grouped object is an instance of a <b>ServiceController<\/b><i> <\/i>object. This output is a bit distracting.<\/p>\n<p>In situations, where the grouping is simple, the <b>Group<\/b> output might actually be useful. An example of this is the grouping of numbers, as shown in the image that follows. (<b>Group<\/b> is an alias for <b>Group-Object<\/b>).<i><\/i><\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2437.HSG-01-11-12-02.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2437.HSG-01-11-12-02.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>If the grouping information does not add any value, omit it by using the <i>NoElement <\/i>switched parameter. The revised command to display the status of services and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5076.HSG-01-11-12-03.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5076.HSG-01-11-12-03.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>One of the cool things to do with the <b>Group-Object <\/b>cmdlet is to use it to return a hash table of information. I have written extensively about hash tables in the past on the Hey, Scripting Guy! Blog; and in fact, I even wrote an entire week of blogs that talked about <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/arrays+hash+tables+and+dictionary+objects\/getting+started\/\" target=\"_blank\">the basics of using hash tables.<\/a><\/p>\n<p>Here are the steps for using the <b>Group-Object <\/b>cmdlet to return a hash table of information:<\/p>\n<ol>\n<li>Pipe the objects to the <b>Group-Object cmdlet<\/b>.<\/li>\n<li>Use the <i>AsHashTable <\/i>switched parameter and the <i>AsString <\/i>switched parameter.<\/li>\n<li>Store the resulting hash table in a variable.<\/li>\n<\/ol>\n<p>An example of using these steps is shown in the code that follows.<\/p>\n<p style=\"padding-left: 30px\">$hash = Get-Service | group status -AsHashTable &ndash;AsString<\/p>\n<p>After it is created, view the hash table by displaying the content that is stored in the variable. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $hash<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Value<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">Running&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {System.ServiceProcess.ServiceController, System.S&#8230;<\/p>\n<p style=\"padding-left: 30px\">Stopped&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {System.ServiceProcess.ServiceController, System.S&#8230;<\/p>\n<p>At this point, the output does not appear to be more interesting than a simple grouping. But, the real power appears when accessing the key properties (those stored under the <b>Name<\/b><i> <\/i>column). To access the objects stored in each of the key values, use dotted notation, as shown here.<\/p>\n<p style=\"padding-left: 30px\">$hash.running<\/p>\n<p>The command to create the hash table of service information and to access the running services by using dotted notation are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3581.HSG-01-11-12-04.jpg\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3581.HSG-01-11-12-04.jpg\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>I can index into the collection by using square brackets and selecting a specific index number. This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $hash.running[5]<\/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; AudioEndpointBu&#8230; Windows Audio Endpoint Builder<b><\/b><\/p>\n<p>If I am interested in a particular running service, I can pipe the results to the <b>Where-Object <\/b>cmdlet (the question mark is an alias for <b>Where-Object<\/b>). This technique is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; $hash.running | ? {$_.name -match &#8220;bfe&#8221;}<\/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; BFE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Base Filtering Engine<\/p>\n<p>In addition to being able to group directly by a property, such as running services, it is also possible to group based on a script block. The script block becomes sort of a <i>where <\/i>clause. To find the number of services that are running, and support a stop command, use the <b>Group-Object <\/b>cmdlet and a script block. This command is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Service | group {$_.status -eq &#8220;running&#8221; -AND $_.canstop}<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Count Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Group<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8211; &#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; 61 True&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {System.ServiceProcess.ServiceController, System&#8230;.<\/p>\n<p style=\"padding-left: 30px\">&nbsp; 115 False&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {System.ServiceProcess.ServiceController, System&#8230;.<\/p>\n<p>&nbsp;<\/p>\n<p>MS, that is all there is to using the <b>Group-Object <\/b>cmdlet to group data. I invite you to join me tomorrow for more Windows PowerShell goodness.<\/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 the Windows PowerShell Group-Object cmdlet to organize data. &nbsp;Hey, Scripting Guy! I have been using Windows PowerShell more these days. I find it really easy to use, and I like the way I can find things. But what I need is a better way to [&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-11511","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: Microsoft Scripting Guy, Ed Wilson, teaches how to use the Windows PowerShell Group-Object cmdlet to organize data. &nbsp;Hey, Scripting Guy! I have been using Windows PowerShell more these days. I find it really easy to use, and I like the way I can find things. But what I need is a better way to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11511","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=11511"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/11511\/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=11511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=11511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=11511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}