liveBook · Manning

Formatting

As explained in Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

The formatting system is PowerShell's subsystem for converting objects into instructions for human-readable textual or onscreen output.

PowerShell formatting converts objects into human-readable screen, file, or hard-copy output. It lets you override default display rules when objects contain more properties than the default view shows. section 11.0 section 11.1

Default views define column headers, widths, alignment, and layout through .format.ps1xml files. Format- cmdlets let you request different output forms, while the formatting system chooses a view from the object type. section 11.2 section 11.9.2

Keep these boundaries in mind:

  • Treat formatting as presentation rather than data transformation; Out-GridView bypasses the formatting subsystem and accepts regular objects, not Format- cmdlet output. section 11.8
  • Avoid mixing unrelated object types in one pipeline. The first object determines the expected formatting, so later objects may display incompletely or fall back to a list. section 11.9.2
  • Use predefined custom views when appropriate; creating custom views requires complicated XML syntax that is not publicly documented. section 11.9.2

Inspecting and changing a default view

Run `Get-Process` to observe formatted columns, then inspect a formatting definition with `cd $pshome/Modules/PSReadLine` and `code PSReadLine.format.ps1xml`. You can also inspect a type with `Get-PSReadLineKeyHandler | Get-Member` and retrieve its formatting data with `Get-FormatData -PowerShellVersion 7.1 -TypeName Microsoft.PowerShell.KeyHandler`. section 11.2

PS /Users/jamesp/> Get-Process; Get-History
 
 NPM(K)    PM(M)      WS(M)     CPU(s)      Id  SI ProcessName
 ------    -----      -----     ------      --  -- -----------
...
      0     0.00       1.74       0.25    1244   1 UsageTrackingAg
      0     0.00       0.68       0.19     710   1 USBAgent
      0     0.00       4.12       6.37     544   1 useractivityd
      0     0.00       5.44       8.00     407   1 UserEventAgent
      0     0.00       7.50       3.43     428   1 usernoted
      0     0.00       3.44       8.71     506   1 ViewBridgeAuxil
      0     0.00       5.91       0.08   27019 ...19 VTDecoderXPCSer
      0     0.00       5.92       0.07   89048 ...48 VTDecoderXPCSer
      0     0.00      10.79      50.02     434   1 WiFiAgent
      0     0.00       1.11       0.20    1242   1 WiFiVelocityAge
      0     0.00      10.28       4.30   20921 ...21 XprotectService
 
Id                 : 1
CommandLine        : Update-TypeData -Path /tmp/Uri.Types.ps1xml
ExecutionStatus    : Completed
StartExecutionTime : 9/21/2019 12:20:03 PM
EndExecutionTime   : 9/21/2019 12:20:03 PM
Duration           : 00:00:00.0688690
 
Id                 : 2
CommandLine        : Update-TypeData -Path /tmp/Uri.Types.ps1xml
ExecutionStatus    : Completed
StartExecutionTime : 9/21/2019 12:21:07 PM
EndExecutionTime   : 9/21/2019 12:21:07 PM
Duration           : 00:00:00.0125330eyp

Compare the readable process table with the degraded output produced when process and history objects share a pipeline. section 11.9.2

How Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS covers it

How deep it goes
Explains default views, type selection, and major pitfalls, but defers advanced combined-object formatting and undocumented custom-view XML.
Examples
Uses Get-Process, Get-History, PSReadLine formatting files, Get-Member, Get-FormatData, and experiments with the three main Format- cmdlets.
What it assumes
Assumes familiarity with PowerShell objects, properties, Get-Member, and Select-Object while building formatting from default output.
What you can do after
Practice/perform custom output formatting and recognize pipeline mixtures that produce unreliable displays.
How it connects
Connects PowerShell objects, object types, pipelines, Format- cmdlets, Select-Object, Out-GridView, and custom views.

Also explained in

Compare how 7 books explain formatting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS. The section links point to the parts of the book it draws on.