CmdID / PowerShell / Cmdlets / Sort
Sort-Object
Sorts objects by property values. PowerShell reference
From the PowerShell reference
6 parameters
Alias sort
What it does
Sorts objects by property values. The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values. If sort properties aren't included in a command, PowerShell uses default sort properties of the first input object. If the input object's type has no default sort properties, PowerShell attempts to compare the objects themselves. For more information, see the Notes section. You can sort objects by a single property or multiple properties. Multiple properties use hash tables to sort in ascending order, descending order, or a combination of sort orders. Properties are sorted as case-sensitive or case-insensitive. Use the Unique parameter to remove duplicates from the output.
Sort-Object [[-Property] <Object[]>] [-Descending] [-Unique] [-InputObject <psobject>] [-Culture <string>] [-CaseSensitive] [<CommonParameters>]
Sort the current directory by name
Get-ChildItem -Path C:\Test | Sort-ObjectIdentifySort the current directory by file length
Get-ChildItem -Path C:\Test -File | Sort-Object -Property LengthIdentifySort processes by memory usage
Get-Process | Sort-Object -Property WS | Select-Object -Last 5IdentifySort HistoryInfo objects by Id
Get-History | Sort-Object -Property Id -DescendingIdentifyUse a hash table to sort properties in ascending and descending order
Get-Service |
Sort-Object -Property @{Expression = "Status"; Descending = $true},
@{Expression = "DisplayName"; Descending = $false}IdentifySort text files by time span
Get-ChildItem -Path C:\Test\*.txt |
Sort-Object -Property {$_.CreationTime - $_.LastWriteTime} |
Format-Table CreationTime, LastWriteTime, FullNameIdentifySort names in a text file
# All items unsorted
Get-Content -Path C:\Test\ServerNames.txtIdentifySort a string as an integer
# String sorted
Get-Content -Path C:\Test\ProductId.txt | Sort-ObjectIdentifySort by multiple properties
Get-ChildItem -Path C:\Test | Sort-Object Length,NameIdentifySort hashtables by their key values with calculated properties
@(
@{ name = 'a' ; weight = 7 }
@{ name = 'b' ; weight = 1 }
@{ name = 'c' ; weight = 3 }
@{ name = 'd' ; weight = 7 }
) | Sort-Object -Property { $_.weight }, { $_.name } -OutVariable Sorted
$Sorted | ForEach-Object -Process { "{0}: {1}" -f $_.name, $_.weight }IdentifyIts page in the reference: Sort-Object.
Parameters
-CaseSensitiveIndicates that the sort is case-sensitive. By default, sorts aren't case-sensitive.-CultureSpecifies the cultural configuration to use for sorts. Use Get-Culture to display the system's culture configuration.-DescendingIndicates that Sort-Object sorts the objects in descending order. The default is ascending order.
To sort multiple properties with different sort orders, use a hash table. For example, with a hash table you can sort one property in ascending order and another property in descending order.-InputObjectTo sort objects, send them down the pipeline to Sort-Object. If you use the InputObject parameter to submit a collection of items, Sort-Object receives one object that represents the collection. Because one object can't be sorted, Sort-Object returns the entire collection unchanged.-PropertySpecifies the property names that Sort-Object uses to sort the objects. Wildcards are permitted. Objects are sorted based on the property values. If you don't specify a property, Sort-Object sorts based on default properties for the object type or the objects themselves.
Use commas to separate multiple properties. Multiple properties can be sorted in ascending order, descending order, or a combination of sort orders. When you specify multiple properties, the objects are sorted by the first property. If multiple objects have the same value for the first property, those objects are sorted by the second property. This process continues until there are no more specified properties or no groups of objects.
The Property parameter's value can be a calculated property. To create a calculated property, use a scriptblock or a hashtable.
Valid keys for a hash table are as follows:
- Expression - <string> or <scriptblock>
- Ascending or Descending - <boolean>
For more information, see about_Calculated_Properties.-UniqueIndicates that Sort-Object eliminates duplicates and returns only the unique members of the collection. The first instance of a unique value is included in the sorted output.
Unique is case-insensitive. Strings that only differ by character case are considered the same. For example, character and CHARACTER.