PowerShell How-To

How To Format PowerShell Output with Format-Table

You may not think too often about how text is displayed in your PowerShell console, but under the covers, there's quite a bit going on.

Think about all of the commands you have the ability to run in PowerShell, with each having thousands of different object names and values. PowerShell can't just return the raw output of all of that stuff. It would be a mess!

Luckily, PowerShell does an excellent job of automatically formatting output for us, but there are times when we want our own method of formatting. One command to change the default format is the Format-Table cmdlet.

To understand how the Format-Table cmdlet works, you'll first need to understand a bit about how PowerShell's formatting type system works. To demonstrate, I'll call the Get-Process cmdlet.

PS C:\> Get-Process | ft

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
     87       5     1108       4728       0.00   1432  13 CExecSvc
     39       4     1680       3024       0.00  10820  13 cmd
     94       8     3696       7716       0.09  11108  13 conhost
    224      11     1872       4688       0.19    908  13 csrss
      0       0        0          4                 0   0 Idle
    810      24     5124      15120       0.17   7824  13 lsass

Notice how it returns seven columns of values. These columns are in a table format versus the list format that you get when using Format-List, as shown below.

PS C:\> Get-Process | Format-List

Id      : 1432
Handles : 85
CPU     : 0
SI      : 13
Name    : CExecSvc

Id      : 10820
Handles : 39
CPU     : 0
SI      : 13
Name    : cmd

Format-Table forces the text to be displayed in a table but Get-Process output is already in tabular format. I could pipe Get-Process directly to Format-Table, but it would have no difference at all in the way the output looks.

However, what if I use Select-Object to get all of the properties that Get-Process returns? By default, Get-Process only shows a few important properties.

You can see below that the output is now in list format because it can't fit all of the properties across the screen.

PS C:\> Get-Process | Select-Object -Property Name,Id,Description,FileVersion,Threads -First 1


Name        : CExecSvc
Id          : 1432
Description : Container Execution Agent
FileVersion : 10.0.14393.0 (rs1_release.160715-1616)
Threads     : {8008, 8688, 6340, 5184}

However, I know that all of those properties will fit on my screen. I need to override the default formatting. This is where Format-Table comes into play. By piping the output of any command such as the one we have here, I can force the output to be in a tabular format.

PS C:\> Get-Process | Select-Object -Property Name,Id,Description,FileVersion,Threads -First 1 | Format-Table

Name       Id Description               FileVersion                            Threads
----       -- -----------               -----------                            -------
CExecSvc 1432 Container Execution Agent 10.0.14393.0 (rs1_release.160715-1616) {8008, 8688, 6340, 5184}

The Format-Table cmdlet is a cmdlet that will always be used last in the pipeline. Like the other formatting cmdlets, Format-Table doesn't return any objects to the pipeline and thus should always be last.

The Format-Table has a lot of different options to manipulate the text once it's displayed, as well. Some common parameters are AutoSize, which automatically sizes the output based on the width of your console, and HideTableHeaders, which doesn't display the object property names.

For a complete breakdown, check out the help content by using Get-Help Format-Table -Detailed. This will break down all of the various incarnations that are possible with the Format-Table cmdlet.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.