Learning PowerShell Format-Table Cmdlet

Adam Bertram

Read more posts by this author.

PowerShell holds a developer’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 via a default format, but it’s up to you to ultimately decide how you’d like output returned. Using PowerShell Format-Table cmdlet, you tweak the output to your liking.

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 Powershell format-table, Format-List and Format-Wide at your disposal. 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.

PS> Get-Item -Path C:\Windows\

Directory: C:\
Mode	LastWriteTime		Length	Name
---- 	------------- 		------	----

d-----	11/24/2017 9:57 AM 			Windows

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

PS> Get-Item -Path C:\Windows\ | Select-Object -Property *

PSPath 				: Microsoft.PowerShell.Core\FileSystem::C:\Windows\
PSParentPath 		: Microsoft.PowerShell.Core\FileSystem::C:\
PSChildName 		: Windows
PSDrive				: C
PSProvider 			: Microsoft.PowerShell.Core\FileSystem
PSIsContainer 		: True
Mode 				: d-----
BaseName 			: Windows
Target 				: {C:\Windows}
LinkType 			:
Name 				: Windows
FullName 			: C:\Windows\
Parent 				:
Exists 				: True
Root 				: C:\
Extension 			:
CreationTime 		: 3/18/2017 5:40:20 AM
CreationTimeUtc 	: 3/18/2017 11:40:20 AM
LastAccessTime 		: 11/24/2017 9:57:51 AM
LastAccessTimeUtc	: 11/24/2017 3:57:51 PM
LastWriteTime 		: 11/24/2017 9:57:51 AM
LastWriteTimeUtc 	: 11/24/2017 3:57:51 PM
Attributes 			: Directory

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 Powershell 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.

PS> Get-Item -Path C:\Windows\ | Select-Object -Property LastAccessTime,Name,LastWriteTime,PSProvider,CreationTime

LastAccessTime 	: 11/24/2017 9:57:51 AM
Name 			: Windows
LastWriteTime 	: 11/24/2017 9:57:51 AM
PSProvider 		: Microsoft.PowerShell.Core\FileSystem
CreationTime 	: 3/18/2017 5:40:20 AM

PS> Get-Item -Path C:\Windows\ | Format-Table -Property LastAccessTime,Name,LastWriteTime,PSProvider,CreationTime

LastAccessTime			Name	LastWriteTime			PSProvider	CreationTime
-------------- 			---- 	------------- 			---------- 	------------
11/24/2017 9:57:51 AM 	Windows 11/24/2017 9:57:51 AM	Microsoft.PowerShell.Core\FileSystem	3/18/2017 5:40:20 AM

Powershell Format-Table output will attempt to display information 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.

Subscribe to Stay in Touch

Never miss out on your favorite ATA posts and our latest announcements!

Looks like you're offline!