Skip to content

PowerShell sorting and comparison

PowerShell provides cmdlets that allow users to order and compare objects based on specific property values. Sorting is typically handled by Sort-Object, which can arrange output in ascending or descending order^[600-developer__learn-powershell01.md].

Sorting Objects

The Sort-Object cmdlet is used to sort objects by one or more properties^[600-developer__learn-powershell01.md].

Syntax and Usage

To sort objects, you can pipe the output of a command to Sort-Object and specify the property to use for sorting.^[600-developer__learn-powershell01.md]

For example, to sort processes by their memory usage, you can pipe the process objects to Sort-Object and specify the -Property parameter followed by the property name^[600-developer__learn-powershell01.md].

Descending Order

By default, Sort-Object sorts in ascending order. To sort in descending order (e.g., from highest to lowest), you must add the -Descending switch parameter^[600-developer__learn-powershell01.md].

The following command demonstrates sorting processes by the PM (Paged Memory) property in descending order^[600-developer__learn-powershell01.md]:

get-process | Sort-Object -Property PM -Descending | Format-Table * -AutoSize

Sources

^[600-developer__learn-powershell01.md]