Home » How to Format Output to a Table in PowerShell

How to Format Output to a Table in PowerShell

In PowerShell, you can format the output as a table using the Format-Table cmdlet. This command allows you to display the output in a table format with customizable properties and column headers.

Method 1: Format Output to table using Format-Output cmdlet

Get-Process | Format-Table -Autosize

This example formats the information about a process retrieved using the Get-Process command in a table format.

Method 2: Format Output to a table by specifying the order

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

This example shows how to customize the table output further by specifying the order of the columns.

Method 3: Format output to a table and output to a file

Get-Process | Format-Table | Out-File -FilePath C:\temp\myprocess.txt

All these methods can be utilized to format the output to a table.

The following examples show how to do it.

Format Output to table using Format-Output cmdlet

The Format-Output cmdlet in PowerShell is used to format the output to a table.

The following example shows how to do it with syntax.

Get-Process | Format-Table -Autosize

In the above PowerShell script, the Get-Process retrieves information about the currently running processes and pipes it to the Format-Table cmdlet.

The Format-Table command formats the output to a table. The AutoSize parameter adjusts the column width to minimize truncation.

The output of the above PowerShell script is given below.

PowerShell format output to table using Format-Table cmdlet
PowerShell format output to a table using Format-Table cmdlet

Format Output to a table by specifying the order

You can customize the table output by specifying the order of the columns, column headers, and other options using the Format-Table parameters.

The following PowerShell script shows how to do it with syntax.

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

The output of the above PowerShell script specifies the order of the properties (columns) in the table.

Format Output to a Table and Output to a File

You can save the output of the command to a file using the Out-File cmdlet in PowerShell.

The following example shows how to do it with syntax.

Get-Process | Format-Table | Out-File -FilePath C:\temp\myprocess.txt

In this example, the Get-Process cmdlet retrieves the process and output to a table using the Format-Table cmdlet. Later, it pipes the output to an Out-File cmdlet to write to a file named myprocess.txt.

Conclusion

I hope the above article on how to format the output to a table in PowerShell using the Format-Table cmdlet is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.