DevOps | Scripts | Automation

PowershellWindows Server

Formatting output in PowerShell

PowerShell often produces output in terms of the object, they are in the default format. For example below command,

Get-Service 

You can see in the above output that only the default properties are displayed and the output format is not proper as the Name property output is truncated because when we write command and press enter, the output is passed to the default pipeline.

Get-Process | Out-Default | Out-Host

The above image depicts how the output is displayed in the console. PowerShell supports few formatting pipeline commands for the output. Below are commonly supported commands for formatting the output object.
a. Format-Table
b. Format-List
c. Format-Wide
There is also Format-Hex and Format-Custom command to filter output but they are out of the scope of this article.

Format-Table command

Format-Table (Alias: ft) is to display the output in the table format. This command will display the full length of the property output that is truncated in the default format. YOu can use -Autosize parameter to adjust the output as per your screen size.

Get-Service | Format-Table -AutoSize

You can also select the specific properties of the PowerShell.

Get-Service | ft Name, Status, StartType

Format-List command

Format-List (Alias: Fl) command is used to show the output in the list format. For example,

Get-Service | Format-List

To display all the properties, use (*) keyword after Format-List.

Get-Service | fl *

You can also filter specific properties using Format-List.

Get-Service | fl Name, DisplayName, Status, Starttype

Format-Wide command

Format-Wide (Alias: fw) command is also the output object formatting command and it shows only a single property. If you don’t provide any property name explicitly then the command will show the default property name. In most of the cases, it is Name property.

Get-Service | Format-Wide

If you need different property output, use that name of the property.

Get-Service | fw -Property DisplayName

You can use -Column parameter to get the output in number of columns in the console.

Get-Process | fw -Property ID -Column 5

There is a –AutoSize parameter supported in this command to get the output formatted properly by auto-sizing its columns. The below command will show only process IDs in the console in a formatted manner.

Get-Process | fw -Property ID -AutoSize