CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

PowerShell – Sort-Object cmdlet – Sorts objects by property values

Displaying the results in sorted order is an essential feature every tool or language has to provide in order to the users to provide the flexibility to play with the data. PowerShell also provides Sort-Object cmdlet to sorts objects by it’s property values.

Sort-Object cmdlet

This cmdlet allows to sort the objects by it’s property values. It allows to sort the objects in ascending or descending order depending on the values of it’s properties.

Sort-Object cmdlet need collection of objects to sort the data. That means, the result of another cmdlet is the input to this cmdlet. We always combine this with another cmdlet by using pipe “|” symbol.

For example, below command sorts the list of processes displayed.

PS C:\> Get-Process | Sort-Object

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     69       9     2148       1912    70     0.14   8032 acrotray
     78       8     1276       1652    42     0.06   3076 armsvc
    143      13    18984      21740    78     0.58   8284 audiodg
    187      24    37044      38700   400     2.98  10140 chrome
...............
...............
   371      19    15984      19476    75    21.89   5456 WmiPrvSE
   189      10     2240       2428    52     0.50   4948 WUDFHost
    62       8     1396       1416    71     0.08   4088 WZQKPICK32

Observe that, the results were sorted on ProcessName. We can mention property names using -Property parameter.

For example, to sort the list of processes based on it’s CPU utilization we have to pass CPU as an argument through the -Property parameter.

PS C:\> Get-Process | Sort-Object -Property CPU

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
      0       0        0         24     0               0 Idle
     45       6     1892       1836    24     0.03   3400 Locator
...............
...............
   397      62   174036     185452   854   102.06  10800 chrome
   437      15     6428       7320    54   109.93    492 svchost
  1080       0      324       1248    13   431.64      4 System
   675      82   120632     110812   398   447.97  10796 iexplore

As I mentioned, we can sort the values in ascending or descending order. By default the order is in ascending. If you want to change the order, you need to specify the parameter -Descending. Below is the example, to display the list of processing in descending order by CPU utilization.

PS C:\> Get-Process | Sort-Object -Property CPU -Descending

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
    672      82   117004     109916   397   480.84  10796 iexplore
   1084       0      324       1484    13   464.54      4 System
    439      15     6384       7768    57   110.68    492 svchost
...............
...............
     0       0        0         24     0               0 Idle

Sort-Object cmdlet also supports multi-key sort; that means, you can use multiple properties to sort the values and each property should be separated by pipe symbol “|” and should be bind with a separate Sort-Object cmdlet.

If you use the multiple -Property parameters with single Sort-Object cmdlet, PowerShell will throw below Error.

PS C:\> Get-Process | Sort-Object -Property CPU -Descending -Property ProcessName
Sort-Object : Cannot bind parameter because parameter 'Property' is specified more than once. 
To provide multiple values to parameters that can accept multiple values, use the array syntax. 
For example, "-parameter value1,value2,value3".
At line:1 char:53
+ Get-Process | Sort-Object -Property CPU -Descending -Property ProcessName
+                                                     ~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Sort-Object], ParameterBindingException
    + FullyQualifiedErrorId : ParameterAlreadyBound,Microsoft.PowerShell.Commands.SortObjectCommand

The correct usage of multi-key sort is; bind each -Property parameter with a separate Sort-Object cmdlet and combine them using pipe “|” symbol. Here is the working example, to sort the list of processes based on the CPU Utilization and ProcessName.

PS C:\> Get-Process | Sort-Object -Property CPU -Descending | Sort-Object -Property ProcessName -Descending

Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName
-------  ------    -----      ----- -----   ------     -- -----------
     62       8     1396       1440    71     0.08   4088 WZQKPICK32
    139      11     4056       5844    47     1.58    224 WmiPrvSE
...............
...............
    77       8     1268       1648    42     0.05   2500 armsvc
   107       9     1700       1868    55     0.16   2536 AGSService
    69       9     2148       1924    70     0.16   8032 acrotray
    72       8     2400       1696    84     0.06   7176 AccelerometerSt

Before I complete this Article, another important parameter we need to know is -CaseSensitive parameter. By default, sorting is NOT case sensitive; if you want to do the case sensitive sort, use this parameter with Sort-Object cmdlet.

We will discuss more features in upcoming Articles.

[..] David

PowerShell – Sort-Object cmdlet – Sorts objects by property values

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top