GUIs 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 parameter, which is an alternative technique to the
Group-Object
cmdlet.
# PowerShell Group Example Clear-Host Get-Process | Group-Object
company # Compared with: Get-Process | Group-Object company | Sort
count -descending
While I favour the full command (above), you can omit '-object' from Group-Object
(just as omitted it from Sort-Object. This shortened version (below) will work:
Get-Process | group company | sort count -descending.
-GroupBy Alternative to Group-Object
Here is a parallel technique, which achieves a slightly different result by using
Format-Table and
its parameter -groupby:
# PowerShell -groupBy Example Clear-Host Get-Process | Sort-Object
company | ` Format-Table name, company -groupby company -auto
Note 1: The backtick (`) causes the command to
word-wrap to the next line.
Guy
Recommends: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems. Fortunately, SolarWinds
have created a
Free WMI Monitor so that you can discover these gems of performance
information, and thus improve your PowerShell scripts. Take the guess work out of which WMI counters to use when scripting the
operating system, Active Directory or Exchange Server.
This Group-Object example employs Get-Service as a vehicle for testing
this PowerShell 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.
# Preliminary Group Example Clear-Host Get-Service | Group-Object status #Compared with Get-Service |
Group-Object status | Format-List
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.
# Sort and Group on Two Criteria Get-Service | Sort-Object status, name ` |
Format-Table -groupby status
# Compared with Clear-Host Get-Service | sort status, name ` |
Ft -groupby status Name, DisplayName, Status -auto
Note 2: These examples sort firstly on 'status', then
secondly on 'name'.
Note 3: Ft is an alias for Format-Table.
Guy Recommends: SolarWinds' Free Bulk Import Tool
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
Sometimes 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:
Group-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.
If you like this page then please share it with your friends
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, SolarWinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.