Scripting PowerShell Group-ObjectIntroduction to Scripting PowerShell Group-ObjectGUIs such as Task Manager lack the ability to group columns or objects. Hence, one of the benefits of using a PowerShell script is that you can append a Group-Object clause, and thus get a more meaningful display of data. I have also included the format-table -groupby parmeter, which is an alternative technique to the group-Object cmdlet. Once we have grouped objects, we can add clarity by appending extra code which sorts the items into numeric or alphabetical order. Observe in the following examples how PowerShell provides sort-Object for sequencing the output. Group-Object Topics♣ Example 1 Process
get-Process | group-Object company
While I favour the full command (above), you can omit '-object' from group-Object. This shortened version (below) will work:
Here is a parallel technique, which achieves a slightly different result by using format-Table and
its parameter -groupby: Example 2 Service - Sorting on Two Criteria
get-Service | group-Object status Again, here is an alternative technique employing format-Table with -groupby to enhance the output. With format-Table you can refine the output by specifying the properties.
get-Service | sort-Object status, name | format-Table -groupby status Note: These examples sort firstly on 'status', then secondly on 'name'. Example 3 EventlogWith get-Eventlog, always remember the name of the log! System, Application, Security or which ever log you are investigating. Example 3a
get-Eventlog system -newest 3000 |group-Object eventid | ` Example 3b
get-Eventlog system -newest 3000 |sort-Object eventid | ` Note: Observe the backtick (`), this is like word-wrap, it tells PowerShell that the command continues on the next line. Further Research on group-ObjectSometimes we get carried away and forget the basics. In the case of PowerShell this means remembering 'help' with the -full parameter. Thus in this instance we need: get-Help group-Object -full To be frank, for once help does not offer much more information, however, this in itself gives peace of mind that there is no more to know about this cmdlet. Another example
of -groupby
The Engineer's Toolset v10 provides a
comprehensive console of utilities for troubleshooting computer problems. Guy says
it helps me monitor what's occurring on the network, and the tools
teaches me more about how the system literally operates. There are so many good gadgets, it's like having free rein of a
sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.
Download your copy of the Engineer's Toolset v 10 Summary Group-ObjectGroup-Object is a useful addition to your PowerShell tool-kit, indeed the ability to control data output is a one reason for employing PowerShell rather than using the GUIs. A typical scenario for group-Object is where you wish to aggregate the data displayed by a PowerShell query. As usual, you are spoilt for choice, the decision lies between piping to group-Object, or alternatively to experiment with format-Table -groupby. See more PowerShell examples for syntax• PowerShell Home • Syntax • ISE (GUI) • Pipeline • Format-table • Group • Sort Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
*
|
||||||