Windows PowerShell


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

 ♣

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: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

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 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.

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2009 Computer Performance LTD All rights reserved

Please report a broken link, or an error.