One of my team members recently tried to sort a table from Format-Table in PowerShell. This is very useful; you will get this requirement as a PowerShell developer. I will explain how to sort with Format-table in PowerShell in this tutorial with examples.
To sort and format data in PowerShell, you can use the Sort-Object and Format-Table cmdlets together. First, sort the data by a specific property using Sort-Object -Property <PropertyName>. Then, format the sorted data into a readable table with Format-Table -Property <Property1>, <Property2> -AutoSize. For example:
$employees | Sort-Object -Property JobTitle | Format-Table -Property Name, JobTitle -AutoSizeThis script sorts employees by job title and displays their names and job titles in a neatly formatted table.
We will use the Format-Table and Sort-Object cmdlets in PowerShell here. Let me show you both cmdlets.
PowerShell Format-Table
The Format-Table cmdlet is used to format the output as a table with the selected properties of an object. This cmdlet is particularly useful when you need to display data in a structured and readable manner.
Syntax
Format-Table [-Property] <Object[]> [-AutoSize] [-Wrap] [<CommonParameters>]- Property: Specifies the properties to be displayed. You can list multiple properties separated by commas.
- AutoSize: Adjusts the column widths based on the data.
- Wrap: Wraps the text within the column.
Example
Let’s say we have a list of employees, and we want to display their names and job titles in a table format.
$employees = @(
[PSCustomObject]@{Name="John Smith"; JobTitle="Software Engineer"},
[PSCustomObject]@{Name="Jane Doe"; JobTitle="Project Manager"},
[PSCustomObject]@{Name="Michael Johnson"; JobTitle="System Administrator"}
)
$employees | Format-Table -Property Name, JobTitle -AutoSizeThis script will output a neatly formatted table with the names and job titles of the employees.
Here is the exact output in the screenshot below:

Check out PowerShell Format-Table Column Width
PowerShell Sort-Object
The Sort-Object cmdlet allows you to sort objects by property values. This is particularly useful when organizing data in a specific order.
Syntax
Sort-Object [-Property] <string[]> [-Descending] [-Unique] [<CommonParameters>]- Property: Specifies the property to sort by.
- Descending: Sorts the data in descending order.
- Unique: Returns only unique values.
Example
Continuing with our list of employees, let’s sort them by their job titles.
$employees = @(
[PSCustomObject]@{Name="John Smith"; JobTitle="Software Engineer"},
[PSCustomObject]@{Name="Jane Doe"; JobTitle="Project Manager"},
[PSCustomObject]@{Name="Michael Johnson"; JobTitle="System Administrator"}
)
$employees | Sort-Object -Property JobTitleThis will sort the employees in ascending order based on their job titles.
Here is the exact output in the screenshot below:

Sort a Table With Format-Table in PowerShell
Now, let me show you how to sort data and format it into a table in PowerShell. We can achieve this by combining both cmdlets.
Here is an example.
$employees = @(
[PSCustomObject]@{Name="John Smith"; JobTitle="Software Engineer"},
[PSCustomObject]@{Name="Jane Doe"; JobTitle="Project Manager"},
[PSCustomObject]@{Name="Michael Johnson"; JobTitle="System Administrator"}
)
$employees | Sort-Object -Property JobTitle | Format-Table -Property Name, JobTitle -AutoSizeThis script first sorts the employees by their job titles and then formats the output into a table.
Here is the exact output in the screenshot below:

Sort by Multiple Properties
You can sort by multiple properties by specifying them in a comma-separated list. Here is an example.
$employees = @(
[PSCustomObject]@{Name="John Smith"; JobTitle="Software Engineer"; Department="IT"},
[PSCustomObject]@{Name="Jane Doe"; JobTitle="Project Manager"; Department="HR"},
[PSCustomObject]@{Name="Michael Johnson"; JobTitle="System Administrator"; Department="IT"},
[PSCustomObject]@{Name="Emily Davis"; JobTitle="Software Engineer"; Department="HR"}
)
$employees | Sort-Object -Property Department, JobTitle | Format-Table -Property Name, JobTitle, Department -AutoSizeThis will first sort the employees by department and then by job title within each department.
Using Calculated Properties
You can also use calculated properties to create custom columns in your table.
$employees | Format-Table -Property Name, JobTitle, @{Label="Name Length"; Expression={$_.Name.Length}} -AutoSizeThis adds a new column that displays the length of each employee’s name.
Check out Convert String to HTML Table in PowerShell
PowerShell Format Table Sort Alphabetically
Sorting data alphabetically in PowerShell using Format-Table involves two main steps: first, sorting the data with Sort-Object, and then formatting it into a table with Format-Table. This process ensures that your data is organized alphabetically by the specified property before being displayed in a structured table format.
let me show you an example.
Let’s consider a list of employees and sort them alphabetically by their names before displaying the data in a table format.
$employees = @(
[PSCustomObject]@{Name="John Smith"; JobTitle="Software Engineer"},
[PSCustomObject]@{Name="Jane Doe"; JobTitle="Project Manager"},
[PSCustomObject]@{Name="Michael Johnson"; JobTitle="System Administrator"},
[PSCustomObject]@{Name="Emily Davis"; JobTitle="Software Engineer"}
)
$employees | Sort-Object -Property Name | Format-Table -Property Name, JobTitle -AutoSizeIn this script:
- Sort-Object -Property Name: This sorts the
$employeesarray alphabetically by theNameproperty. - Format-Table -Property Name, JobTitle -AutoSize: This formats the sorted list into a table displaying the
NameandJobTitleproperties, adjusting the column widths for better readability.
The result is a neatly formatted table with employee names sorted alphabetically, making it easier to read and analyze the data.
I executed the above PowerShell script using VS code, and you can see the output in the screenshot below:

In this tutorial, I used this real example to explain how to use the Sort With Format-Table in PowerShell.
You may also like the following tutorials:
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.