liveBook · Manning

Formatting

As explained in Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

A formatting system is the subsystem that applies display rules to structured data and generates instructions for rendering it.

A formatting system turns structured PowerShell objects into display instructions. It chooses tables or lists, supplies headers, and controls presentation without changing the underlying objects. section 3.4

Use formatting as a presentation layer rather than embedding display work in commands:

  • Return useful objects from tools and let the formatting system decide how they appear. section 3.4
  • Create a default view when output should consistently appear as a table or expose selected properties. section 24.1 section 24.2
  • Define advanced views with XML-based format files and connect them to a module manifest. section 24.4

Default view for disk information

A Get-DiskInfo function returns computer, domain controller, manufacturer, model, and memory properties. A formatting view can render these objects as a table automatically, without piping to Format-Table or altering the returned data. section 24.1 section 24.2

Listing 24.1 Starting point for this chapter
function Get-DiskInfo {
 foreach ($domain in (Get-ADForest).domains) { 
   $hosts = Get-ADDomainController -filter * -server $domain | 
   Sort-Object -Prop hostname
   ForEach ($h in $hosts) { 
    $cs = Get-CimInstance -ClassName Win32_ComputerSystem `
                         # -ComputerName $h
    $props = @{'ComputerName' = $h
               'DomainController' = $h
               'Manufacturer' = $cs.manufacturer
               'Model' = $cs.model
               'TotalPhysicalMemory(GB)'=$cs.totalphysicalmemory / 1GB
               }
     New-Object -Type PSObject -Prop $props
   } #foreach $h
  } #foreach $domain
} #function
Export-ModuleMember -function Get-DiskInfo

Inspect the Get-DiskInfo implementation and its object-producing output. section 24.1

How Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools covers it

How deep it goes
Descends from automatic table-or-list selection to default views, XML format files, and module manifests.
Examples
Uses Get-DiskInfo output containing computer, domain controller, hardware, model, and memory properties.
What it assumes
Assumes familiarity with PowerShell objects, properties, pipelines, commands, and modules.
What you can do after
Configure default and custom views that improve presentation without changing command output objects.
How it connects
Connects objects, pipelines, default views, Format-Table, XML format files, and PowerShell modules.

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 Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools. The section links point to the parts of the book it draws on.