Chapter 11: Formatting: And why it’s done on the right
This introduction recaps how object properties and PowerShell’s defaults affect the output readers have seen so far.
After this chapter you will be able to
- Object properties and default output. Explain that cmdlets produce objects with properties PowerShell may not show by default, and distinguish inspecting or selecting properties from relying on PowerShell’s defaults for the final appearance of output.
- Formatting and useful reports. Explain how formatting helps turn information collected with PowerShell into output suitable for reports, without treating PowerShell as a full-fledged management-reporting tool.
- Objects to displayed output. Trace how objects from a command become output on the screen or in a text file through Out- cmdlets and the formatting system.
- Predefined views by object type. Explain how the formatting system first looks for a predefined view matching an object's type and what such a view can specify.
- Fallback properties and layout. Describe how PowerShell chooses displayed properties and a table or list when no predefined view matches.
- Table property selection. Explain how Format-Table’s -Property parameter selects table columns, including its positional use and support for wildcards.
- Grouping table rows. Describe what -GroupBy changes in a table and why sorting by the same property first helps.
- Wrapping truncated table values. Explain when to use -Wrap and its effect on table output.
- List formatting and property values. Explain why a list can be preferable to a table when information will not fit horizontally, and describe how Format-List displays an object’s properties and their values.
- Format-Wide property and columns. Describe what Format-Wide displays, its property restriction, and how its default display can be changed.
- Custom formatting entries. Explain how Format-Table and Format-List use hash table constructs to create custom columns or list entries, including a label that differs from the property name.
- Calculated process memory column. Trace how the Get-Process example displays VM memory as a whole-number VM(MB) column.
- Formatting-specific display keys. Distinguish the additional hash table keys accepted by formatting commands from the Name and Expression keys used with Select-Object, and describe what the display keys control.
- Formatted output destinations. Trace where formatting instructions go when a command ends in a Format- cmdlet, and describe how to send them to the screen or a file explicitly.
- GridView output and pipeline input. Explain that Out-GridView displays command results in a graphical interface, bypasses PowerShell’s formatting subsystem, and accepts regular objects rather than the output of a Format- cmdlet.
- Format Cmdlet Pipeline Placement. Explain why a Format- cmdlet should be last in a pipeline, except when followed by Out-File. Describe what happens if a later command expects the original objects.
- Mixed object formatting. Explain how the formatting system chooses a format and why output can become less useful when a pipeline contains different kinds of objects. Describe what the process-and-history example illustrates.
What it builds on
- Discovering objects: Get-Member (chapter 8) · Using Get-Member to discover an object’s properties.
- Selecting the properties you want (chapter 8) · Using Select-Object to choose properties to display.
- Connecting one command to another: Less work for you (chapter 6) · Commands pass results through a pipeline.
- What are objects? (chapter 8) · PowerShell commands produce objects.
- Using object attributes, or properties (chapter 8) · Properties are attributes of objects.
- Sorting objects (chapter 8) · Sorting objects by a property
- When things don’t line up: Custom properties (chapter 10) · Introduces hash table constructs for custom properties.
Section by section
Introduction
This introduction recaps how object properties and PowerShell’s defaults affect the output readers have seen so far.
- Object properties and default output: Explain that cmdlets produce objects with properties PowerShell may not show by default, and distinguish inspecting or selecting properties from relying on PowerShell’s defaults for the final appearance of output.
Why it matters: It helps you distinguish the data an object contains from the output PowerShell chooses to show.
Key terms: cmdlets, objects, properties, gm, Select-Object, default configuration and rules
11.1 Formatting: Making what you see prettier
Introduces formatting as the way to turn information collected with PowerShell into useful output, while noting that PowerShell is not a full-fledged reporting tool.
- Formatting and useful reports: Explain how formatting helps turn information collected with PowerShell into output suitable for reports, without treating PowerShell as a full-fledged management-reporting tool.
Why it matters: It connects formatting to the practical task of presenting information PowerShell has collected.
Key terms: formatting, output, reports
11.2 Working with the default formatting
This section explains how PowerShell turns objects into default text output, how it chooses a display, and why displayed column headers may differ from property names.
- Objects to displayed output: Trace how objects from a command become output on the screen or in a text file through Out- cmdlets and the formatting system.
Why it matters: It explains why the objects a command produces and the text you see are not the same thing. - Predefined views by object type: Explain how the formatting system first looks for a predefined view matching an object's type and what such a view can specify.
Why it matters: A predefined view explains why some object types have a carefully arranged default display. - Fallback properties and layout: Describe how PowerShell chooses displayed properties and a table or list when no predefined view matches.
Why it matters: The number of properties selected for display can determine the layout even when no special view exists.
Key terms: Out-Default, Out-Host, Out-File, formatting system, formatting instructions, predefined view, .format.ps1xml, object type, table view, default display property set
11.3 Formatting tables
This section explains how to use Format-Table to choose table columns, group rows by a property, and preserve long values in the display.
- Table property selection: Explain how Format-Table’s -Property parameter selects table columns, including its positional use and support for wildcards.
Why it matters: Choosing properties lets you control which columns a table attempts to show. - Grouping table rows: Describe what -GroupBy changes in a table and why sorting by the same property first helps.
Why it matters: Sorting first helps rows with the same value appear together under a group heading. - Wrapping truncated table values: Explain when to use -Wrap and its effect on table output.
Why it matters: Wrapping lets you read values that a compact table would cut off.
Key terms: Format-Table, ft, -Property, wildcards, -GroupBy, property, -Wrap, ellipses
11.4 Formatting lists
Explains when to use Format-List and how it can reveal an object’s properties and their values.
- List formatting and property values: Explain why a list can be preferable to a table when information will not fit horizontally, and describe how Format-List displays an object’s properties and their values.
Why it matters: A list lets you inspect information that is awkward to display in a table.
Key terms: Format-List, fl, -Property, Format-Table, gm
11.5 Formatting wide lists
This section explains how Format-Wide displays one property in a multicolumn list and how to change its column count.
- Format-Wide property and columns: Describe what Format-Wide displays, its property restriction, and how its default display can be changed.
Why it matters: This helps you choose Format-Wide when you want a compact display of one property.
Key terms: Format-Wide, fw, -Property, Name, -Columns
11.6 Creating custom columns and list entries
Shows how formatting hash tables create custom table columns and list entries, including calculated values and display settings.
- Custom formatting entries: Explain how Format-Table and Format-List use hash table constructs to create custom columns or list entries, including a label that differs from the property name.
Why it matters: You can tailor what a command displays without relying on its default headings. - Calculated process memory column: Trace how the Get-Process example displays VM memory as a whole-number VM(MB) column.
Why it matters: The example shows how a calculated column can make raw property values easier to read. - Formatting-specific display keys: Distinguish the additional hash table keys accepted by formatting commands from the Name and Expression keys used with Select-Object, and describe what the display keys control.
Why it matters: These keys let you refine the appearance of a table without changing the value-producing expression. - Multiline command completion: Describe why PowerShell waits after an incomplete command and how to finish or abandon the multiline example.
Why it matters: Recognizing the changed prompt helps you complete a long command or escape an accidental unfinished one.
Key terms: Format-Table, Format-List, hash table, custom columns, custom list entries, Get-Process, VM, VM(MB), 1 MB, -as
11.7 Going out: To a file or to the host
This section explains how formatted output reaches the screen or a file, and why file output may need a wider character width.
- Formatted output destinations: Trace where formatting instructions go when a command ends in a Format- cmdlet, and describe how to send them to the screen or a file explicitly.
Why it matters: Choosing the output destination lets you display formatted results or save them as a file. - File output width: Explain why formatted output in a text file may differ from its onscreen appearance and how to accommodate wider tables.
Why it matters: Adjusting file output width can keep wider tables readable when saved.
Key terms: Format- cmdlet, formatting instructions, Out-Default, Out-Host, Out-File, -Width, character width
11.8 Another out: GridViews
This section introduces Out-GridView as graphical output that takes regular objects rather than formatted output.
- GridView output and pipeline input: Explain that Out-GridView displays command results in a graphical interface, bypasses PowerShell’s formatting subsystem, and accepts regular objects rather than the output of a Format- cmdlet.
Why it matters: It helps you choose the right pipeline input when you want a graphical view of results. - GridView availability across versions: Describe where Out-GridView came from and how the PowerShell 6+ cross-platform version is made available.
Why it matters: It explains why Out-GridView may not already be available in a newer PowerShell installation.
Key terms: Out-GridView, formatting subsystem, Format- cmdlets, regular objects, Windows PowerShell, PowerShell 6+, PowerShell Gallery, module
11.9 Common points of confusion
Introduces two common formatting pitfalls that the following subsections will explain.
11.9.1 Always format right
This subsection explains why formatting belongs at the end of a PowerShell pipeline and why Out-File is the only useful exception.
- Format Cmdlet Pipeline Placement: Explain why a Format- cmdlet should be last in a pipeline, except when followed by Out-File. Describe what happens if a later command expects the original objects.
Why it matters: Formatting too early changes what downstream commands receive. - Out GridView Input Distinction: Distinguish Out-GridView from an Out- cmdlet that can receive formatting instructions, and explain why it should not follow a Format- cmdlet.
Why it matters: The Out- prefix alone does not mean a command can display formatted output.
Key terms: Format- cmdlet, formatting instructions, Out-File, Out-Default, Out-Host, Out-GridView, standard objects
11.9.2 One type of object at a time, please
This section explains why mixing object types in one pipeline can produce uneven formatting, while noting that predefined custom views are an exception.
- Mixed object formatting: Explain how the formatting system chooses a format and why output can become less useful when a pipeline contains different kinds of objects. Describe what the process-and-history example illustrates.
Why it matters: Keeping object types in mind helps you understand why otherwise valid command output may look wrong. - Predefined custom view exception: Distinguish the general warning about mixed object types from the predefined custom view that makes directory listings legible.
Why it matters: The directory-listing example shows that the warning has a specific exception, not that mixed types are always formatted badly.
Key terms: formatting system, pipeline, process objects, history objects, DirectoryInfo, FileInfo, predefined custom formatting view, Format-Custom
11.10 Lab
11.11 Lab answers
11.12 Further exploration
The section encourages readers to practice creating different output formats with the three main Format- cmdlets and their commonly used parameters.
Where this is used later
- One script, one pipeline (chapter 19) · PowerShell’s default formatting of output objects.
- Write-Host (chapter 17) · Assumes familiarity with PowerShell's table-formatting commands.
- Using regex with Select-String (chapter 21) · Formatting output as a table
- Dealing with WMI dates (chapter 26) · Creating custom columns for displayed output.
- Write-Output (chapter 17) · How pipeline objects can be sent to the host for display.
Concepts explained in this chapter
- formattingThe formatting system is PowerShell's subsystem for converting objects into instructions for human-readable textual or onscreen output.
- data conversionCasting is the operation of treating a value as a specified type, optionally converting it to an object of that type.
- default display property setA default display property set is a declared collection of object properties selected for default presentation.
- format fileA format file is a PowerShell XML configuration file that defines how specific object types are displayed.
- viewA predefined view is a stored set of formatting rules targeted at a particular object type.
- Format-TableFormat-Table is a PowerShell cmdlet that displays objects in a tabular format using selected properties or calculated expressions.
- format-listFormat-List is a PowerShell cmdlet that displays selected properties and their values for objects in a list-oriented layout.
- expressionAn expression is a PowerShell construct that evaluates to a value.
- Out-FileOut-File is a PowerShell cmdlet that writes command output to a file.
- object typeAn object type is the runtime classification that determines an object's available structure and behavior.
- column headerA column header is the displayed label identifying a column in tabular output, which may differ from the underlying property name.
- atomic typeA data type defines the kind of value represented and the operations applicable to that value.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
