The top three tips for effective sorting in PowerShell

Summary: Learn the top three tips to effectively use Windows PowerShell to sort data in this blog post by the Microsoft Scripting Guy, Ed Wilson.

One of the fundamental things that I need to do when I look at any kind of data is sort it so that I can make sense of what I am looking at. It doesn’t matter if I am looking at Yelp for restaurant recommendations (I want to sort by distance from me and by average rating) or at my Facebook timeline (I want to sort by time, as in, when an update was posted). Yes, sorting is one thing that makes the difference between, say, looking for books at the library via the online catalog or trying to look for books in the card catalog. I mean, dude, it is painful to go all the way back to using a card catalog. I so want to look for books according to my sort rules, not the default sorts in a card catalog (title, author, or subject).

Don’t need to group before you sort

The first tip to successfully sort data is that I don’t need to group my data before I sort it. The Sort-Object cmdlet does a comparison of the specified property before it orders the output according to the way that I specify. Here is an example:

Get-Process | Sort-Object -Property name

The output and command are shown here:

Screenshot of the Get-Process | Sort-Object -Property name command and output.

To sort in reverse order, use -Descending

If I want to sort in reverse order, I use the -Descending switch. In the example here, I sort by process name, but I do not want the sort in alphabetic order. I want it in reverse alphabetic order:

Get-Process | Sort-Object -Property name -Descending

The command and output are shown here:

Screenshot of the Get-Process | Sort-Object -Property name -Descending command and output.

Sometimes you need more than one property

So, if I sort my process names by name, I end up with a sorted list of process names, but everything else is jumbled. Here is the command:

Get-Process | Sort-Object -Property name

And, here is a portion of the output. Note that the process IDs are not sorted:

Screenshot of output that sorts only the process names.

So that I also have sorted process IDs, I add a second sort parameter. This is shown here:

Get-Process | Sort-Object -Property name, id

Screenshot of output that sorts process names and IDs.

What if I want the process names sorted alphabetically, but I also want the process IDs sorted in reverse order? Well, that is actually pretty easy to do. I just use a hash table for the property parameter. This is shown here:

Get-Process | Sort-Object -Property @{E="Name"; Ascending=$True},@{E="ID";Ascending=$false}

And here is the output:

Screenshot of output that shows process names sorted alphabetically and process IDs sorted in reverse order.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. Also check out my Microsoft Operations Management Suite Blog. See you tomorrow. Until then, peace.

Ed Wilson
Microsoft Scripting Guy

Comments (1)

  1. What does ‘E’ means ?
    @{E=”Name”; Ascending=$True}

Skip to main content