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
$sortedNamesOutput:
Emily Davis
Jane Doe
John Smith
Michael JohnsonIn 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.

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
$sortedUsersOutput:
FirstName LastName
--------- --------
Emily Davis
Jane Doe
Michael Johnson
John SmithIn this example, the Sort-Object cmdlet sorts the user objects based on their LastName property.
Here is the exact output in the screenshot below:

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
$sortedUsersDescendingOutput:
FirstName LastName
--------- --------
John Smith
Michael Johnson
Jane Doe
Emily DavisIn 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
$uniqueSortedNamesOutput:
Emily Davis
Jane Doe
John Smith
Michael JohnsonIn 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
$caseSensitiveSortedNamesOutput:
Emily Davis
Jane Doe
john smith
michael johnsonIn 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
$sortedUsersMultiplePropertiesOutput:
FirstName LastName
--------- --------
Emily Davis
Anna Doe
Jane Doe
Michael Johnson
John SmithIn 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;

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
$sortedFilesByDateOutput:
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.txtIn 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
$sortedProductsByPriceOutput:
ProductName Price
----------- -----
Monitor 300
Tablet 600
Smartphone 800
Laptop 1200In 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:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.