PowerShell Format-Table [With Examples]

As a PowerShell developer, you must format output data for readability and clarity; for this, Format-Table is very useful. In this tutorial, I will explain everything about PowerShell Format-Table with examples.

The Format-Table cmdlet in PowerShell is used to format the output of commands as a table, making the data easier to read and interpret. You can specify which properties to display using the -Property parameter, and adjust column widths to fit the data with the -AutoSize parameter. For example, to list processes with their names, IDs, and CPU usage, you can use: Get-Process | Format-Table Name, Id, CPU -AutoSize. This command ensures the output is neatly organized and easily readable.

What is PowerShell Format-Table?

Format-Table is a cmdlet in PowerShell that formats the output as a table. It’s incredibly useful when displaying data in a structured format, making it easier to interpret. This cmdlet is particularly helpful when dealing with objects and their properties.

Syntax of Format-Table

Here’s the basic syntax of Format-Table:

Format-Table [-Property] <Object[]> [-AutoSize] [-HideTableHeaders] [-Wrap] [<CommonParameters>]

Parameters

  • -Property: Specifies the properties of the objects to be displayed.
  • -AutoSize: Adjusts the column widths based on the actual data.
  • -HideTableHeaders: Hides the table headers.
  • -Wrap: Wraps the text in a column if it exceeds the column width.

Check out Create an HTML Table from Variables in PowerShell

PowerShell Format-Table Examples

Now, let me show you how to use Format-Table PowerShell cmdlet with some real examples.

1. Basic Example

Let’s start with a basic example. Suppose you want to list the processes running on your computer. You can use the Get-Process cmdlet and pipe its output to Format-Table:

Get-Process | Format-Table

This command will display a table with the default properties of the process objects.

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

PowerShell Format-Table

2. Select Specific Properties

Often, you may only be interested in specific properties. For example, to display the process name, ID, and CPU usage, you can specify these properties:

Get-Process | Format-Table Name, Id, CPU

Check out Convert String to HTML Table in PowerShell

3. Auto-Size Columns

By default, PowerShell sets the column width based on the property names. However, you can use the -AutoSize parameter to adjust the column width based on the data:

Get-Process | Format-Table Name, Id, CPU -AutoSize

Here is the exact output in the screenshot below:

Format-Table PowerShell

4. Hide Table Headers

If you prefer to hide the table headers, you can use the -HideTableHeaders parameter:

Get-Process | Format-Table Name, Id, CPU -HideTableHeaders

5. Wrap Text

When dealing with long text values, the -Wrap parameter can be useful. This parameter wraps the text within the column width:

Get-Process | Format-Table Name, Id, CPU -Wrap

Check out Format an Array as an HTML Table in PowerShell

6. Customize Column Names

You can also customize the column names using calculated properties. For example, let’s say you want to display the process name as “ProcessName” and the ID as “ProcessID”:

Get-Process | Format-Table @{Label="ProcessName";Expression={$_.Name}}, @{Label="ProcessID";Expression={$_.Id}}, CPU

Here is the exact output in the screenshot below:

PowerShell Format-Table Examples

7. Sort Data

To sort the data, you can use the Sort-Object cmdlet before piping the output to Format-Table in PowerShell. For instance, to sort processes by CPU usage:

Get-Process | Sort-Object CPU -Descending | Format-Table Name, Id, CPU -AutoSize

8. Display Nested Properties

Sometimes, you might need to display nested properties. For example, to display the user and domain of a process owner:

Get-Process | Format-Table Name, Id, @{Label="User";Expression={$_.StartInfo.UserName}}

Check out PowerShell Match Operator Examples

9. Filter Data

Suppose you want to list all services on your computer along with their status and display names:

Get-Service | Format-Table DisplayName, Status, ServiceType -AutoSize

You can also filter the data before formatting it. For example, to display only running services:

Get-Service | Where-Object {$_.Status -eq 'Running'} | Format-Table DisplayName, Status, ServiceType -AutoSize

Conclusion

In this tutorial, I explained how to use the Format-Table PowerShell cmdlet to present data in a clear and structured format.

I have also shown some real examples of the PowerShell Format-Table cmdlet.

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.