PowerShell Formatting Output
When objects reach the end of the pipeline, PowerShell turns them into text for the screen. You can control that with the Format cmdlets: Format-Table, Format-List and Format-Wide, plus Out-String and Out-File to capture the text.
The default view
PowerShell decides how to show an object based on its type. For custom objects the rule is simple: up to 4 properties → table, 5 or more → list:
[pscustomobject]@{ Host = 'web01'; Cpu = 4; RamGB = 16 }
[pscustomobject]@{ Host = 'db01'; Cpu = 16; RamGB = 128; Disk = '2TB'; Os = 'Linux' }
Host Cpu RamGB
---- --- -----
web01 4 16
db01 16 128
Built-in types such as files, processes and services have predefined views stored in format files, which is why Get-ChildItem shows Mode, LastWriteTime, Length and Name even though a file object has dozens of properties.
Format-Table
Format-Table (alias ft) shows the properties you name as columns. -AutoSize fits the column widths to the data:
$servers = @(
[pscustomobject]@{ Name = 'web01'; Role = 'frontend'; Cpu = 4; RamGB = 16; Location = 'Oslo' }
[pscustomobject]@{ Name = 'db01'; Role = 'database'; Cpu = 16; RamGB = 128; Location = 'Bergen' }
[pscustomobject]@{ Name = 'cache'; Role = 'redis'; Cpu = 2; RamGB = 32; Location = 'Oslo' }
)
$servers | Format-Table Name, Role, RamGB
$servers | Format-Table -Property Name, Cpu, Location -AutoSize
Name Role RamGB
---- ---- -----
web01 frontend 16
db01 database 128
cache redis 32
Name Cpu Location
---- --- --------
web01 4 Oslo
db01 16 Bergen
cache 2 Oslo
Calculated columns and grouping
Columns can be calculated, with a label, a format string and an alignment. -GroupBy prints a separate table per value (sort first!):
$servers = @(
[pscustomobject]@{ Name = 'web01'; RamGB = 16; Location = 'Oslo' }
[pscustomobject]@{ Name = 'db01'; RamGB = 128; Location = 'Bergen' }
[pscustomobject]@{ Name = 'cache'; RamGB = 32; Location = 'Oslo' }
)
$servers | Format-Table Name,
@{ Label = 'RAM (MB)'; Expression = { $_.RamGB * 1024 }; FormatString = 'N0'; Align = 'Right' }
$servers | Sort-Object Location | Format-Table Name, RamGB -GroupBy Location
Name RAM (MB)
---- --------
web01 16,384
db01 131,072
cache 32,768
Location: Bergen
Name RamGB
---- -----
db01 128
Location: Oslo
Name RamGB
---- -----
web01 16
cache 32
Format-List and Format-Wide
Format-List (alias fl) prints one Property : Value line per property, which is best for objects with many or long properties. Format-List * shows all properties, a good way to explore an object. Format-Wide shows one property in several columns:
$user = [pscustomobject]@{ Login = 'ada'; FullName = 'Ada Lovelace'; Team = 'Research'; Admin = $false }
$user | Format-List
$user | Format-List Login, Team
$planets = 'Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn' |
ForEach-Object { [pscustomobject]@{ Name = $_ } }
$planets | Format-Wide Name -Column 3
Login : ada
FullName : Ada Lovelace
Team : Research
Admin : False
Login : ada
Team : Research
Mercury Venus Earth
Mars Jupiter Saturn
Format last!
Format cmdlets output formatting instructions, not your original objects. After a Format-* command, the data is gone for any further processing:
$rows = @(
[pscustomobject]@{ Name = 'b'; Size = 2 }
[pscustomobject]@{ Name = 'a'; Size = 1 }
)
($rows | Format-Table | Select-Object -First 1).GetType().Name
($rows | Sort-Object Name | Select-Object -First 1).Name # correct: sort, then formatFormatStartData
a
So always put Format-Table or Format-List at the very end of a pipeline, and never pipe it into Export-Csv or ConvertTo-Json.
Capturing formatted text: Out-String and Out-File
When you do need the formatted text, for an email body or a report file, use Out-String (into a variable) or Out-File (into a file). -Width controls the line width:
$disks = @(
[pscustomobject]@{ Drive = '/'; UsedPct = 71 }
[pscustomobject]@{ Drive = '/data'; UsedPct = 93 }
)
$report = $disks | Format-Table -AutoSize | Out-String
"The report has $(($report.Trim() -split "`n").Count) lines"
$disks | Format-Table -AutoSize | Out-File ./disk-report.txt
Get-Content ./disk-report.txt | Where-Object { $_.Trim() }The report has 4 lines
Drive UsedPct
----- -------
/ 71
/data 93
Note: The width of the console decides where tables are cut off or wrapped. Scripts that write reports should use
Out-File -Width 200(orOut-String -Width) so that the result doesn't depend on the window size. Machine-readable files belong in CSV or JSON, covered next.
Test yourself
A custom object has 6 properties. How is it displayed by default?
- As a table
- As a list
- As JSON
- Only the first property
What's wrong with Get-ChildItem | Format-Table Name | Export-Csv files.csv?
- Nothing
- Format-Table outputs formatting objects, so the CSV contains formatting data instead of file names
- Export-Csv needs -Path
- Format-Table can't select columns
Which command turns formatted output into a single string?
- Out-String
- ConvertTo-String
- Format-String
- Write-Host