PowerShell Format-Table Views


Format views are defined inside the *format.ps1xml files and represent named sets of properties per type which can be used with any of the Format-* cmdlets. In this post I will focus mainly on the views for the Format-Table cmdlet:

Get-Process | select -Last 5 | Format-Table
Get-Process | select -Last 5 | Format-Table -View StartTime

screenShotFormatView1 Retrieving the format views for a particular type can be accomplished by pulling out the information from the respective XML files like so:

function Get-FormatView{
$formatFiles = dir $psHome -Filter *.format.ps1xml
if ($TypeName -isnot [string]){
$TypeName = $Input[0].PSObject.TypeNames[0]
$formatTypes = $formatFiles |
Select-Xml //ViewSelectedBy/TypeName |
where { $_.Node.'#text' -eq $TypeName }

foreach ($ft in $formatTypes) {
$formatType = $ft.Node.SelectSingleNode('../..')
$props = $formatType.Name, ($formatType | Get-Member -MemberType Property | where Name -like '*Control').Name
$formatType | select @{n='Name';e={$props[0]}},
@{n='Type';e={'Format View'}},
@{n='Cmdlet';e={'Format-' + $props[1].Replace('Control','')}}


With this function the existing format views (for any of the Format-* cmdlets) can be easily discovered:

Get-Process | Get-FormatView | Format-Table -Auto

Creating a new Format-Table view can be done using the Add-FormatTableView function defined below:

function Add-FormatTableView {
param (




[Management.Automation.Alignment] $Alignment = 'Undefined',

$ViewName = 'TableView'
$typeNames = $input | group { $_.PSTypeNames[0] } -NoElement | select -ExpandProperty Name
$table = New-Object Management.Automation.TableControl
$row = New-Object Management.Automation.TableControlRow
$index = 0
foreach ($prop in $property){
if ($Label.Count -lt $index+1){
$currLabel = $prop
$currLabel = $Label[$index]
if ($Width.Count -lt $index+1){
$currWidth = @($Width)[0]
$currWidth = $Width[$index]
if ($Alignment.Count -lt $index+1){
$currAlignment = @($Alignment)[0]
$currAlignment = $Alignment[$index]
$col = New-Object Management.Automation.TableControlColumn $currAlignment, (New-Object Management.Automation.DisplayEntry $prop, 'Property')
$header = New-Object Management.Automation.TableControlColumnHeader $currLabel, $currWidth, $currAlignment
$view = New-Object System.Management.Automation.FormatViewDefinition $ViewName, $table
foreach ($typeName in $typeNames){
$typeDef = New-Object System.Management.Automation.ExtendedTypeDefinition $TypeName
$xmlPath = Join-Path (Split-Path $profile.CurrentUserCurrentHost)  ($ViewName + '.format.ps1xml')
Get-FormatData -TypeName $TypeNames | Export-FormatData -Path $xmlPath
Update-FormatData -PrependPath $xmlPath

Add-TableFormatView can be used to create custom table format views (you guessed it):

$fileName = Get-Process | Add-FormatTableView -Label ProcName, PagedMem, PeakWS -Property 'Name', 'PagedMemorySize', 'PeakWorkingSet' -Width 40 -Alignment Center -ViewName RAM
Get-Process | Format-Table -View RAM
#add this to the profile to have the format view available in all sessions
#Update-FormatData -PrependPath $fileName

screenShotFormatTableView3 This creates a custom format table view for the System.Diagnostics.Process type. The function determines the typename(s) based on the objects piped into it. Some details about the other parameters:

  • The Label property is optional if there is no label provided for a property the property name is used for the label.
  • The Width property is also optional and defaults to 20.
  • The Alginment can be defined per column/property or for all columns if only one alignment is specified, the default Alignment is ‘Undefined’.
  • If the ViewName is not specified it defaults to ‘TableView’. ‘TableView’ determine the default output format for the type (if no default is available) this can be handy to customize the output for custom objects.

The usage of the ‘TableView’ view name to modify custom object default output deserves another example:

#create a custom object array
$arr = @()
$obj = [PSCustomObject]@{FirstName='Jon';LastName='Doe'}
#add a custom type name to the object
$typeName = 'YouAreMyType'
$arr += $obj
$obj = [PSCustomObject]@{FirstName='Pete';LastName='Smith'}
$arr += $obj
#add a default table format view
$arr | Add-FormatTableView -Label 'First Name', 'Last Name' -Property FirstName, LastName -Width 30 -Alignment Center
#when using the object further down in the pipeline the property name must be used
$arr  | where "LastName" -eq 'Doe'

This can come in handy for custom object output since the output is still an object and not a format it can be used further down the pipeline (using the property name and not the label).

Get-FromatView and Add-FormatTableView can be downloaded from my GitHub repository.


Photo Credit: jDevaun.Photography via Compfight cc

I'd love to hear what you think

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s