How To Sort Array Of Objects In PowerShell?

Sorting an array of objects in PowerShell is a common task you will get while working with PowerShell. In this PowerShell tutorial, I will explain how to sort an array of objects in PowerShell using different methods and examples to make it easier for you to understand.

To sort an array of objects in PowerShell, use the Sort-Object cmdlet, specifying the property to sort by with the -Property parameter. For example, $sortedObjects = $arrayOfObjects | Sort-Object -Property PropertyName will sort the objects in ascending order based on PropertyName. For descending order, add the -Descending switch.

Understanding Array of Objects in PowerShell

In PowerShell, everything is an object. An object is a data structure that stores data (properties) and code (methods). When we talk about sorting an array of objects, we are referring to a collection of these data structures that we want to order based on one or more of their properties.

Sort Array Of Objects In PowerShell with Sort-Object

The primary cmdlet for sorting objects in PowerShell is Sort-Object. The Sort-Object cmdlet sorts objects in ascending or descending order based on object property values.

Basic Sorting

Here’s a simple example of how to sort an array of objects by a single property:

$people = @(
    [PSCustomObject]@{Name='Alice'; Age=34},
    [PSCustomObject]@{Name='Bob'; Age=28},
    [PSCustomObject]@{Name='Charlie'; Age=25}
)

$sortedPeople = $people | Sort-Object -Property Age
$sortedPeople

In this example, $people is an array of custom objects with properties Name and Age. We use Sort-Object to sort this array by the Age property.

I executed the PowerShell script using VS code, and you can see the output in the screenshot below:

Sort Array Of Objects In PowerShell

Sort Array of Objects Descending Order

To sort an array of objects in descending order in PowerShell, you can use the -Descending switch:

$people = @(
    [PSCustomObject]@{Name='Alice'; Age=34},
    [PSCustomObject]@{Name='Bob'; Age=28},
    [PSCustomObject]@{Name='Charlie'; Age=25}
)

$sortedPeopleDesc = $people | Sort-Object -Property Age -Descending
$sortedPeopleDesc

You can see the output in the screenshot below:

Sort Array of Objects Descending Order

Sorting by Multiple Properties

You can also sort by multiple properties by passing a list of property names to the -Property parameter. Here is a complete PowerShell script to sort an array of objects by multiple properties in PowerShell.

$people = @(
    [PSCustomObject]@{FirstName='Alice'; LastName='Smith'; Age=34},
    [PSCustomObject]@{FirstName='Alice'; LastName='Johnson'; Age=30},
    [PSCustomObject]@{FirstName='Bob'; LastName='Smith'; Age=28}
)
$sortedPeople = $people | Sort-Object -Property FirstName, LastName
$sortedPeople

In this example, the PowerShell array of objects is first sorted by FirstName and then by LastName.

Sort Array Of Objects In PowerShell with Custom Expressions

You can sort based on custom expressions. For example, if you want to sort by the length of the Name property:

$people = @(
    [PSCustomObject]@{FirstName='Alice'; LastName='Smith'; Age=34},
    [PSCustomObject]@{FirstName='Alice'; LastName='Johnson'; Age=30},
    [PSCustomObject]@{FirstName='Bob'; LastName='Smith'; Age=28}
)
$sortedPeople = $people | Sort-Object -Property { $_.Name.Length }
$sortedPeople

Sort Array Of Objects In PowerShell with Hash Tables

You can use hash tables to specify the direction for each property individually. Here is a PowerShell script to sort an array of objects in PowerShell with a hash table.

$people = @(
    [PSCustomObject]@{FirstName='Alice'; LastName='Smith'; Age=34},
    [PSCustomObject]@{FirstName='Alice'; LastName='Johnson'; Age=30},
    [PSCustomObject]@{FirstName='Bob'; LastName='Smith'; Age=28}
)
$sortedPeople = $people | Sort-Object -Property @{Expression='FirstName'; Descending=$false}, @{Expression='Age'; Descending=$true}
$sortedPeople

In this example, FirstName is sorted in ascending order, while Age is sorted in descending order.

Case-Insensitive Sorting of Array of Objects

By default, sorting is case-sensitive. To perform a case-insensitive sort, you can use a custom expression like below to sort an array of objects in PowerShell.

$people = @(
    [PSCustomObject]@{FirstName='Alice'; LastName='Smith'; Age=34},
    [PSCustomObject]@{FirstName='Alice'; LastName='Johnson'; Age=30},
    [PSCustomObject]@{FirstName='Bob'; LastName='Smith'; Age=28}
)
$sortedPeople = $people | Sort-Object -Property { $_.Name.ToLower() }
$sortedPeople

Sort Array Of Objects In PowerShell [with Null Values]

When sorting a PowerShell array of objects, you might encounter null values. By default, null values are considered to be less than any other value. If you want to treat nulls as greater than any other value, you can use a custom expression:

$sortedPeople = $people | Sort-Object -Property { if ($_.Age -eq $null) { [int]::MaxValue } else { $_.Age } }
$sortedPeople

Sorting Array of Objects from a Pipeline in PowerShell

Often, you’ll be sorting objects that come from another cmdlet. For example, you might want to sort files by their last write time:

$sortedFiles = Get-ChildItem -Path C:\ | Sort-Object -Property LastWriteTime
$sortedFiles

Conclusion

Sorting an array of objects in PowerShell is easy with the Sort-Object cmdlet. In this PowerShell tutorial, I have also explained sorting an array of objects in PowerShell by a single property, multiple properties, or using custom expressions. With the examples provided, you should now have a good understanding of how to sort an array of objects in PowerShell.

You may also like:

>