Sort-Object by Property, Type or Scriptblock

6 min read · Updated 12 November 2026

Did this guide help you? You can support me by subscribing to the channel, sharing it, disabling your ad blocker or making a donation. Thank you!

Hello,

Sort-Object allows you to sort and also filter unique values.

The cmdlet offers sorting options that you may not have taken the time to explore.

Here is a summary:

# Sort processes in ascending order based on the cpu property.
Get-Process | Sort-Object -Property cpu
# Sort processes in descending order using the cpu property
Get-Process | Sort-Object -Property name -descending
# Sort processes in descending order by name property and then by cpu property (for processes with the same name)
Get-Process | Sort-Object -Property name, cpu -descending
# Sort processes in ascending order by name property and then by cpu property (for processes with the same name) in descending order
Get-Process | Sort-Object -Property @{ Expression = "Name"; Descending = $true },
@{ Expression = "DisplayName"; Ascending = $true }
# Sort objects according to the result of a scriptblock (in the example, according to the duration between the creation date and the last modification date)
Get-ChildItem |
Sort-Object -Property @{ Expression = { $_.LastWriteTime - $_.CreationTime }; Descending = $true } |
select-object -Property LastWriteTime, CreationTime
# Convert the object type to change the sorting behaviour of Sort-Object
# Create an array containing string values
[string[]]$array = 9,10,9999,88888,789,2
$array | Sort-Object
# Ask Sort-Object to sort the objects using a particular type, [INT] in my example
$array | Sort-Object -Property { [int]$_ }
# Another example with a [DateTime] type
'Aug 16, 2022', 'Feb 1, 1992', 'June 15, 1980' | Sort-Object
'Aug 16, 2022', 'Feb 1, 1992', 'June 15, 1980' | Sort-Object -Property { [DateTime]$_ }
# Another example with a [version] type
'11.12.13.14', '234.56.78.10', '5.5.5.5' | Sort-Object
'11.12.13.14', '234.56.78.10', '5.5.5.5' | Sort-Object -Property { [version]$_ }
# Keep unique values (delete duplicates) without case sensitivity
'localhost','LOCALHOST','pc1','pc2','localhost','LOCALHOST' | Sort-Object -Unique
# Keep unique values (remove duplicates) with case sensitivity
'localhost','LOCALHOST','pc1','pc2','localhost','LOCALHOST' | Sort-Object -Unique -CaseSensitive
# Sort randomly
1..10 | Sort-Object -Property { Get-Random }
# Delete duplicates with case sensitivity
$array = 'C', 'A', 'B', 'C', 'c', 'C'
$array | Sort-Object -Unique -CaseSensitive
# Delete duplicates without case sensitivity
$array = 'C', 'A', 'B', 'C', 'c', 'C'
$array | Sort-Object -Unique
# Sort values in any order (in the example by colour name, red then green then blue)
$DemoSort = @(
[PSCustomObject]@{ Colour = 'Red'; Value = 8 }
[PSCustomObject]@{ Colour = 'Blue'; Value = 9 }
[PSCustomObject]@{ Colour = 'Green'; Value = 10 }
[PSCustomObject]@{ Colour = 'Blue'; Value = 11 }
[PSCustomObject]@{ Colour = 'Green'; Value = 12 }
)
$DemoSort | Sort-Object {
switch ($_.Colour) {
'Red' { 1 }
'Green' { 2 }
'Blue' { 3 }
}
}
# Example: free space (in GB) per drive, in a hashtable
$disk = @{ 'C:' = 120; 'D:' = 450; 'E:' = 80; 'F:' = 300 }
# A hashtable enters the pipeline as a single object: Sort-Object has nothing to sort
$disk | Sort-Object
# Sort a hashtable by key (in the example by drive name)
$disk.GetEnumerator() | Sort-Object -Property Key
# Sort a hashtable by value in descending order (in the example by free space)
$disk.GetEnumerator() | Sort-Object -Property Value -Descending
# Keep the result in an ordered hashtable
$sorted = [ordered]@{}
$disk.GetEnumerator() | Sort-Object -Property Value -Descending | ForEach-Object { $sorted[$_.Key] = $_.Value }
# Get a dictionary that is always sorted by key
$dict = [System.Collections.Generic.SortedDictionary[string,int]]::new()
$disk.GetEnumerator() | ForEach-Object { $dict.Add($_.Key, $_.Value) }
# Equivalent elements are sorted in order of arrival (Powershell 7)
1..20 | Sort-Object {$_ % 3}
1..20 | Sort-Object {$_ % 3} -Stable
# Display only the first 6 (Powershell 7, including -stable)
1..20 | Sort-Object -Top 6
# Display only the last 6 (Powershell 7, including -stable)
1..20 | Sort-Object -bottom 6

Related links

Follow me on

Support me

Buy Me a Coffee at ko-fi.com

New site · Reference

The Windows directory tree

Native Windows folders, files and logs: what they are for, what they contain, with commands and Microsoft sources.

Browse the tree

New site · Reference

Group Policy settings

ADMX settings for Windows 10, 11, Server and applications: registry keys, PowerShell commands and use cases.

Browse the policies