Learning How to Format PowerShell Output With Format-Table

Format-TablePowerShell holds a scripter’s hand quite a bit, but it can’t read your mind. It can’t inherently know what kind of output you’re looking for. It will provide its best guess, but it’s up to you to ultimately decide how you’d like output returned.

PowerShell has a formatting system that has default output and the ability to change how output is formatted on an object type basis. If you’d rather not learn about XML and how to always format output in a specific way, you’ve also got cmdlets like Format-Table, Format-List and Format-Wide at your disposable. A PS1XML file on the file system or using any of the format cmdlets will display output in some different ways.

Without using any special formatting, PowerShell, by default, uses its format. Whenever a specific object is returned, you’ll probably only see a limited portion of what that actual object’s contents are. For example, just looking a single folder you’ll only see the parent directory and a few attributes.

But, pipe that same command to Select-Object -Property * and see what happens.

PowerShell has hidden a lot of the object’s properties from you because usually, you don’t need to see all of this information. PowerShell provides a default view, but that doesn’t mean that’s your only option. We can change up this view via a Format cmdlet.

The most popular formatting cmdlet is Format-Table. According to the PowerShell help:

The Format-Table cmdlet formats the output of a command as a table with the selected properties of the object in each column. The object type determines the default layout and properties that are displayed in each column, but you can use the Property parameter to select the properties that you want to see.

You can also use a hash table to add calculated properties to an object before displaying it and to specify the column headings in the table. To add a calculated property, use the Property or GroupBy parameter.

Using Format-Table and Select-Object to pick out individual object properties appears to be same as long as the property values don’t extend past the current console’s width. But as soon as you can attempt to display lots of objects, you’ll see that Format-Table sticks with the tabular format while Select-Object reverted to displaying the properties on each line.

Format-Table will attempt to display the output via a tabular format always. If the property values are too long to be displayed, it will replace any missing text with ellipses to indicate more information is available but just not shown.

The Format-Table cmdlet has a lot of different ways it can manipulate output. Check out the full documentation of Format-Table via Microsoft docs.