Ezine 189 – PowerShell Sort-Object and -GroupBy

Introduction to PowerShell Sort-Object and -GroupBy

It’s so much easier to read a list when the columns are sorted into a meaningful order.  As PowerShell interprets a script, so it often has to deal with raw data which is not sequenced either numerically or alphabetically.  Consequently, a good programmer will add an instruction to sort or group the columns on the most important property.

Topics for PowerShell Sort-Object

Example 1: Rank on DisplayName by Using Sort-Object

To this day, I still have trouble finding certain Windows services, for example, where in the list is the firewall?  Why can’t I find the print spooler under the letter ‘P’?   My memory and experience tell me these services are there somewhere, but what does Microsoft call them?  The answer is to focus not on ‘Name’, but ‘DisplayName’ and then apply sort-Object to this property.

Before we start experimenting with sorting and grouping the output, let me introduce the cmdlet which will be the vehicle for our study, namely: get-Service.  Incidentally, the nouns in PowerShell’s cmdlets are always singular hence service (and not serviceS).

get-Service | Sort-Object DisplayName

Note 1:  If you disregard PowerShell’s pipe (|) then sort-Object won’t work.  The idea is that we get a raw list of services and then squirt them into the sort-Object cmdlet; naturally, we provide the name of the property that is going to act as the primary key for our sort, in this example we will use DisplayName.

Note 2:  You can abbreviate sort-Object to plain sort thus: get-service | sort displayName

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets.  IPAT cracks this problem of allocating IP addresses in networks in two ways:

For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges. 

For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker

Background Research with Get-Member

Never miss an opportunity to run get-Member (gm) against any cmdlet that you are studying.  In the case of get-Service, preceding it with get-Member reveals interesting properties such as ‘DependentServices’, ‘ServiceDependsOn’ and CanStop.  Do remember the pipe symbol thus:

get-Service | get-Member

Example 2: Sort-Object with Format-Table and -GroupBy

Armed with information from get-Member about the additional properties of Windows services, we need a method of selecting the names and position of these extra columns in the output, for this we append the Format-Table cmdlet.  Once again, this example illustrates how piping the output of the first command, becomes the input of the second section, and then onwards into the script’s third element.  I find it helpful to think of successive pipes as refining the output.

The key to understanding -GroupBy is to realize that it’s a parameter, which is associated with Format-Table (and other sister cmdlets).  There are two minor traps with -GroupBy, firstly understand how it differs from Sort-Object.  Secondly, appreciate that -GroupBy is a parameter different from the group-Object cmdlet.

get-Service | Sort-Object Status, DisplayName | `
format-Table DisplayName, CanStop, Status -groupBy status -auto

Note: There is a lot going on in the above example.  Realize that we can sort on more than one property, firstly Status and secondly DisplayName. Also observe how this example does not just sort, but also groups the services into those which are running and those which are stopped.

Make my day:  Choose different properties for Format-Table.  Also experiment with omitting Status from the Sort-Object pipe and appreciate how messy the output gets when you don’t sort on the property that you also want to group.

One more challenge:  Which cmdlet supports -auto?  Try putting get-Help before get-Service.  Now try help before Format-Table.  Any sign of -AutoSize?   My not-so-hidden agenda is to encourage you to research the full list of parameters available to the cmdlets that you are using.  In this instance you may unearth the -descending parameter, which is handy for correcting a list that is in the reverse order that you had hoped.  When in default mode, sort-object builds a list in rising order, this is why we don’t need to explicitly add an ascending instruction.  For even more control over the output sequence, type: help sort-Object -full, and then study example 5.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary of Windows PowerShell’s Sort-Object and -GroupBy

For the user, sorting and grouping data make raw data that bit easier to read.  For the programmer, ranking the output gives satisfaction and a sense of a job well done.  In terms of learning PowerShell, the secret is to understand the difference between the cmdlet sort-Object and the parameter -GroupBy.  As usual, get-Member and get-help… -full have crucial role in researching the full power of any cmdlet that you employ.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell tutorials

PowerShell Tutorials  • Methods  • Cmdlets  • PS Snapin  • Profile.ps1  • Exchange 2007

Command & Expression Mode  • PowerShell pipeline (|)  • PowerShell ‘where‘  • PowerShell ‘Sort’

Windows PowerShell Modules  • Import-Module  • PowerShell Module Directory 

If you see an error of any kind, do let me know.  Please report any factual mistakes, grammatical errors or broken links.