PowerShell Sort-Object Cmdlet
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed Group-Object cmdlet in PowerShell.
https://cloudaffaire.com/powershell-group-object-cmdlet/
In this blog post, we will discuss the Sort-Object cmdlet in PowerShell. You can use the Sort-Object cmdlet to sort the output or objects in ascending or descending order based on object property values supplied to Sort-Object cmdlet. If no object property mentioned, Sort-Object sorts the result based on 1st input object it gets. Sort-Object cmdlet can sort on a single object property or multiple object properties. By default, the Sort-Object cmdlet sorts objects in ascending order. You can also use the Unique parameter to eliminate duplicates from the output of Sort-Object.
Sort-Object Cmdlet Syntax:
1 2 3 4 5 6 7 8 9 |
## Sort-Object ## [-Stable] | -Top ## [-Descending] ## [-Unique] ## [-InputObject ## [[-Property] ## [-Culture ## [-CaseSensitive] ## [ |
Sort-Object Cmdlet Argument List:
- –Bottom: Specifies the number of objects to get from the end of a sorted object array.
- –CaseSensitive: Indicates that the sort is case-sensitive. By default, sorts are not case-sensitive.
- –Culture: Specifies the cultural configuration to use for sorts.
- –Descending: Indicates that Sort-Object sorts the objects in descending order. The default is ascending order.
- –InputObject: To sort objects, send them down the pipeline to Sort-Object.
- –Property: Specifies the property names that Sort-Object uses to sort the objects.
- –Stable: The sorted objects are delivered in the order they were received when the sort criteria are equal.
- –Top: Specifies the number of objects to get from the start of a sorted object array.
- –Unique: Indicates that Sort-Object eliminates duplicates and returns only the unique members of the collection.
PowerShell Sort-Object Cmdlet:
Sort Objects In Ascending Order Based On Single Property In PowerShell:
1 2 3 4 5 6 7 8 |
####################################### ## PowerShell | Cmdlet | Sort-Object ## ####################################### ## PowerShell Latest Version (5) ## sort objects in ascending order Get-Process | Sort-Object -Property ProcessName | Select-Object ProcessName, WorkingSet64 |
Sort Objects In Descending Order Based On Single Property In PowerShell:
1 2 3 |
## sort objects in descending order Get-Process | Sort-Object -Property ProcessName -Descending | Select-Object ProcessName, WorkingSet64 |
Sort Objects Based On Multiple Properties In PowerShell:
1 2 3 |
## sort objects on multiple properties Get-Process | Sort-Object -Property @{Expression = "ProcessName"; Descending = $True}, @{Expression = "WorkingSet64"; Descending = $False} | Select-Object ProcessName, WorkingSet64 |
Remove Duplicates From Sorted Objects In PowerShell:
1 2 3 |
## get unique sorted data Get-Process | Sort-Object -Property ProcessName -Descending -Unique | Select-Object ProcessName, WorkingSet64 |
Hope you have enjoyed this article. In the next blog post, we will discuss the Where-Object cmdlet in PowerShell.
To get more details on PowerShell, kindly follow below official documentation