PowerShell - Sorting with Sort-Object

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make 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]@{ Color = 'Green'; Value = 10 }
[PSCustomObject]@{ Color = 'Blue'; Value = 11 }
[PSCustomObject]@{ Color = 'Green'; Value = 12 }
)
$DemoSort | Sort-Object {
switch ($_.Colour) {
'Red' { 1 }
'Green' { 2 }
'Blue' { 3 }
}
}
# 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