PowerShell Sort-Object [With Examples]

In this tutorial, I will explain how to use the PowerShell Sort-Object cmdlet to organize and manipulate data effectively. I will show you a few examples of PowerShell Sort-Object cmdlet here.

PowerShell Sort-Object

The Sort-Object cmdlet in PowerShell is used to sort objects based on their properties. This cmdlet is useful when you need to organize data in a specific order, such as sorting a list of users by their last names or arranging files by their creation dates in PowerShell.

Now, with some examples, let me show you the syntax and how to use the PowerShell Sort-Object cmdlet.

Syntax of Sort-Object

The basic syntax of the Sort-Object cmdlet is as follows:

Sort-Object [-Property] <String[]> [-Descending] [-Unique] [-Culture <String>] [-CaseSensitive] [<CommonParameters>]
  • -Property: Specifies the property to sort by.
  • -Descending: Sorts the data in descending order.
  • -Unique: Removes duplicate entries.
  • -Culture: Specifies the culture to use for sorting.
  • -CaseSensitive: Performs a case-sensitive sort.

Now, let me show you a few examples to help you understand how to use the PowerShell Sort-Object cmdlet.

Check out PowerShell Read-Host

PowerShell Sort-Object Examples

Here are some examples of Sort-Object in PowerShell.

Example-1: Sort a List of Names

Let’s start with a simple example. Suppose you have a list of names, and you want to sort them alphabetically. Here’s how you can do it using the Sort-Object cmdlet.

$names = @("John Smith", "Jane Doe", "Michael Johnson", "Emily Davis")
$sortedNames = $names | Sort-Object
$sortedNames

Output:

Emily Davis
Jane Doe
John Smith
Michael Johnson

In this example, the Sort-Object cmdlet sorts the list of names in ascending order by default.

I executed the above PowerShell script, and you can see the exact output in the screenshot below also.

PowerShell Sort-Object

Read PowerShell Get-Process

Example-2: Sort by a Specific Property

Now, let’s consider a more complex example where you have a list of user objects, and you want to sort them by their last names.

$users = @(
    [PSCustomObject]@{FirstName="John"; LastName="Smith"},
    [PSCustomObject]@{FirstName="Jane"; LastName="Doe"},
    [PSCustomObject]@{FirstName="Michael"; LastName="Johnson"},
    [PSCustomObject]@{FirstName="Emily"; LastName="Davis"}
)

$sortedUsers = $users | Sort-Object -Property LastName
$sortedUsers

Output:

FirstName LastName
--------- --------
Emily     Davis
Jane      Doe
Michael   Johnson
John      Smith

In this example, the Sort-Object cmdlet sorts the user objects based on their LastName property.

Here is the exact output in the screenshot below:

PowerShell Sort-Object examples

Read PowerShell Compare-Object

Example-3: Sort in Descending Order

If you want to sort the data in descending order, you can use the -Descending parameter. Here’s an example:

$sortedUsersDescending = $users | Sort-Object -Property LastName -Descending
$sortedUsersDescending

Output:

FirstName LastName
--------- --------
John      Smith
Michael   Johnson
Jane      Doe
Emily     Davis

In this example, the Sort-Object cmdlet sorts the user objects in descending order based on their LastName property.

Example-4: Remove Duplicates

Sometimes, you might have duplicate entries in your data, and you want to remove them while sorting. You can use the -Unique parameter to achieve this. Here’s an example:

$namesWithDuplicates = @("John Smith", "Jane Doe", "Michael Johnson", "Emily Davis", "Jane Doe")
$uniqueSortedNames = $namesWithDuplicates | Sort-Object -Unique
$uniqueSortedNames

Output:

Emily Davis
Jane Doe
John Smith
Michael Johnson

In this example, the Sort-Object cmdlet removes the duplicate entry “Jane Doe” and sorts the remaining names.

Read PowerShell Import-Csv Cmdlet

Example-5: Case-Sensitive Sort

By default, the Sort-Object cmdlet performs a case-insensitive sort. If you want to perform a case-sensitive sort, you can use the -CaseSensitive parameter. Here’s an example:

$namesWithCase = @("john smith", "Jane Doe", "michael johnson", "Emily Davis")
$caseSensitiveSortedNames = $namesWithCase | Sort-Object -CaseSensitive
$caseSensitiveSortedNames

Output:

Emily Davis
Jane Doe
john smith
michael johnson

In this example, the Sort-Object cmdlet sorts the names in a case-sensitive manner, placing “john smith” and “michael johnson” after the capitalized names.

Example-6: Sort by Multiple Properties

You can also sort data by multiple properties. For example, if you have a list of user objects and you want to sort them by their last names and then by their first names, you can do the following:

$usersWithSameLastName = @(
    [PSCustomObject]@{FirstName="John"; LastName="Smith"},
    [PSCustomObject]@{FirstName="Jane"; LastName="Doe"},
    [PSCustomObject]@{FirstName="Michael"; LastName="Johnson"},
    [PSCustomObject]@{FirstName="Emily"; LastName="Davis"},
    [PSCustomObject]@{FirstName="Anna"; LastName="Doe"}
)

$sortedUsersMultipleProperties = $usersWithSameLastName | Sort-Object -Property LastName, FirstName
$sortedUsersMultipleProperties

Output:

FirstName LastName
--------- --------
Emily     Davis
Anna      Doe
Jane      Doe
Michael   Johnson
John      Smith

In this example, the Sort-Object cmdlet first sorts the user objects by their LastName property and then by their FirstName property.

Here is the exact output in the screenshot below;

how to use PowerShell Sort-Object

Read PowerShell Set-Content cmdlet

Example-7: Sort Files by Creation Date

Another common use case for the Sort-Object cmdlet is sorting files by their creation dates. Here’s an example:

$files = Get-ChildItem -Path "C:\Users\Public\Documents"
$sortedFilesByDate = $files | Sort-Object -Property CreationTime
$sortedFilesByDate

Output:

    Directory: C:\Users\Public\Documents

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a----        4/10/2024  10:15 AM           12345 example1.txt
-a----        5/10/2024   2:30 PM           67890 example2.txt
-a----        6/09/2024   9:45 AM           13579 example3.txt

In this example, the Sort-Object cmdlet sorts the files in the specified directory by their creation dates.

Example-8: Sort Custom Objects

You can also create custom objects and sort them using the Sort-Object cmdlet. Here’s an example:

$products = @(
    [PSCustomObject]@{ProductName="Laptop"; Price=1200},
    [PSCustomObject]@{ProductName="Tablet"; Price=600},
    [PSCustomObject]@{ProductName="Smartphone"; Price=800},
    [PSCustomObject]@{ProductName="Monitor"; Price=300}
)

$sortedProductsByPrice = $products | Sort-Object -Property Price
$sortedProductsByPrice

Output:

ProductName Price
----------- -----
Monitor     300
Tablet      600
Smartphone  800
Laptop      1200

In this example, the Sort-Object cmdlet sorts the custom product objects by their Price property.

Conclusion

The Sort-Object cmdlet in PowerShell is used to organize and manipulate data. You can use the Sort-Object cmdlet to sort a list of names, user objects, files, or custom objects.

In this tutorial, I explained the basic syntax of the Sort-Object cmdlet and explored various examples to demonstrate its usage. From sorting by specific properties to handling case sensitivity and removing duplicates, the Sort-Object cmdlet can be used for various PowerShell needs

Do let me know in the comment below if this tutorial helps you.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.