Partager via


Using the Sort-Object Cmdlet

Sorting Returned Data

If you’re a script writer you’ve no doubt fantasized about being able to easily sort data any way you want to. Who said dreams never come true? In Windows PowerShell it’s amazingly easy to sort data, and to sort it any way you want.

For example, by default the Get-EventLog cmdlet returns data sorted by the time the event was written to the event log. But what if you’d prefer to sort data by the EventID. What then?

What then, indeed. To sort by EventID just retrieve your data and then pipe it to the Sort-Object cmdlet, telling Sort-Object which property to sort on:

Get-EventLog system -newest 5 | Sort-Object eventid

Run that command, and you’ll get back data similar to this:

Index Time          Type Source                EventID Message
----- ----          ---- ------                ------- -------
 6194 May 16 12:41  Info W32Time                    35 The time service is n...
 6193 May 16 12:41  Erro Dhcp                     1000 Your computer has los...
 6197 May 16 14:47  Warn Dhcp                     1003 Your computer was not...
 6196 May 16 14:13  Warn Dhcp                     1003 Your computer was not...
 6195 May 16 12:42  Warn Dhcp                     1007 Your computer has aut...

Nice, huh? Would you rather have the events sorted in descending order; that is, with the largest EventID listed first? No problem; just tack on the -descending parameter:

Get-EventLog system -newest 5 | Sort-Object eventid -descending

Notice the change:

Index Time          Type Source                EventID Message
----- ----          ---- ------                ------- -------
 6195 May 16 12:42  Warn Dhcp                     1007 Your computer has aut...
 6197 May 16 14:47  Warn Dhcp                     1003 Your computer was not...
 6196 May 16 14:13  Warn Dhcp                     1003 Your computer was not...
 6193 May 16 12:41  Erro Dhcp                     1000 Your computer has los...
 6194 May 16 12:41  Info W32Time                    35 The time service is n...

Windows PowerShell also allows you to sort by multiple properties: just separate the properties using commas. For example, suppose you retrieve a list of all the files in the folder C:\Scripts. You’d like to first sort the data by file extension and then by file size; in other words, you want to group all the .txt files together, then sort that little grouping by file size. Here’s the command (using the Get-ChildItem cmdlet) that can pull that off:

Get-ChildItem c:\scripts | Sort-Object extension,length

And here’s the kind of output you’ll get. Notice that not only are all the .vbs files listed together, but they are also arranged by file size, with the smallest .vbs file listed first and the largest .vbs file listed last:

-a---          5/5/2006   9:09 PM      53358 tee.txt
-a---         5/15/2006   8:57 AM        377 new_excel.vbs
-a---         3/15/2006  10:23 AM        399 imapi.vbs
-a---          3/3/2006   2:46 PM        537 methods.vbs
-a---          3/3/2006   2:55 PM        698 read-write.vbs
-a---         3/11/2006  10:10 PM        978 imapi2.vbs
-a---         3/17/2006   8:21 AM       1105 winsat.vbs
-a---          5/5/2006   2:55 PM      19225 test.vbs
-a---          4/4/2006   8:30 AM    2616487 HoneyPie.wma
Sort-Object Aliases
  • sort