Scripting PowerShell Group-ObjectIntroduction to Scripting PowerShell Group-ObjectMany GUIs 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 -groupby, which is an alternative technique to group-Object. Once we have grouped objects, it often adds clarity if we add 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 TopicsExample 1 Process
get-Process | group-Object company
While I favour the full command (above), you can omit the 'get'. You can also omit the '-object' from group-Object. This shortened version (below) will work:
Here is a parallel technique, which achieves the same result but using format-Table and -groupby: Example 2 Service
get-Service | group-Object status Again, here is an alternative technique, observe how format-Table with -groupby can enhance the output. With format you can refine the output by specifying the properties. Format-table's, sister command is format-list.
get-Service | sort-Object status | format-Table -groupby status Example 3 EventlogWith get-Eventlog, always remember the name of the log! System, Application, Security or which ever log you are investigating. get-Eventlog system -newest 3000 |group-Object eventid |sort-Object count -descending |format-Table count, name -autosize. get-Eventlog system -newest 3000 |sort-Object eventid | where {$_.EntryType -eq "Error"} |format-Table -groupby eventid, EntryType, Message.
˚
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 Microsoft PowerShell syntax• PowerShell Home • Syntax • -f format • Pipeline • Quotes • Format-table • Group • Select-String 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.
*
|
||||||