PowerShell Sort-Object and -GroupByIt'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-ObjectTo 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-MemberNever 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 -GroupByArmed 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 | ` 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 v10
|
||||||
Download my ebook:
|
*
|
|
|
|
Home Copyright © 1999-2009 Computer Performance LTD All rights reserved Please report a broken link, or an error. | |