Chapter 6: The pipeline: Connecting commands
The introduction contrasts ordinary command execution with PowerShell’s ability to connect commands in one-line sequences.
After this chapter you will be able to
- Connected command sequences. Explain that PowerShell runs individual commands much like other shells, but stands out because it lets you connect multiple commands in powerful one-line sequences.
- Connecting Commands with Pipelines. Explain how the pipe symbol connects two commands so the second can work with the first command’s output, using Dir | more as an example.
- Purpose of exporting command output. Explain why you might export PowerShell output instead of only viewing it on screen, using Get-Process and CSV as an example.
- Exporting pipeline output to CSV. Explain how piping Get-Process to Export-CSV creates a CSV file and how the output filename is supplied.
- Nested process thread exports. Explain why JSON is a better choice than CSV when exporting a process’s nested Threads property.
- JSON conversion and retrieval. Describe the roles of ConvertTo-Json, Out-File, and ConvertFrom-Json in saving and getting process data back.
- CLIXML object round-trip. Explain that Export-Clixml writes a PowerShell-oriented XML file and Import-Clixml reads it back, allowing PowerShell to reconstruct the original objects or something very close. State that the commands require a filename.
- CLIXML format trade-offs. Justify why CLIXML is not always the best export format: its files are often larger, its PowerShell-specific content can be hard for other languages to read, and Windows encrypts security-related portions so only the creating user or machine can decrypt them.
- Out-File pipeline output. Explain that Out-File takes data from the pipeline and writes it to a flat file, such as a text file.
- Snapshot comparison workflow. Describe how to save a reference snapshot in CLIXML, then compare its imported data with current Get-Process output using Compare-Object. Explain why the saved snapshot can serve as a configuration baseline.
- Process comparison results. Explain why the example compares process names rather than complete process data, and interpret the indicators in Compare-Object’s output.
- File output and width. Explain that > is a shortcut for sending output to a file through Out-File, and describe why running Out-File explicitly can be useful.
- Default screen output path. Trace where output goes when no other Out- cmdlet is specified.
- HTML conversion and saving. Describe how piping command output to ConvertTo-Html produces browser-readable HTML, and how piping that result to Out-File saves it as a text file.
- ConvertTo and Export distinction. Distinguish ConvertTo- cmdlets from Export cmdlets, and explain why a conversion-only command can be useful even when an Export command is shorter for saving a file.
- Pipelines into modifying cmdlets. Explain how output from Get-Process can be passed to Stop-Process, and why passing every process is dangerous.
- Automatic and requested confirmation. Describe when the shell automatically asks for confirmation and how to request a prompt with -Confirm.
- Previewing changes with WhatIf. Explain what -WhatIf does when used with a cmdlet that could change the system.
- Raw text versus imported data. Distinguish reading an exported file with Get-Content from reading it with the matching Import cmdlet. Explain when to use each approach for CSV or XML files.
What it builds on
- The anatomy of a command (chapter 4) · The basic form of a command with a cmdlet name and parameters.
- Using help to find commands (chapter 3) · Using PowerShell's command-discovery tools.
Section by section
Introduction
The introduction contrasts ordinary command execution with PowerShell’s ability to connect commands in one-line sequences.
- Connected command sequences: Explain that PowerShell runs individual commands much like other shells, but stands out because it lets you connect multiple commands in powerful one-line sequences.
Why it matters: This distinction sets up why the chapter focuses on connecting commands.
Key terms: cmdlet, parameters, one-line sequences
6.1 Connecting one command to another: Less work for you
This section introduces the pipeline as a way to pass one PowerShell command’s output to another command.
- Connecting Commands with Pipelines: Explain how the pipe symbol connects two commands so the second can work with the first command’s output, using Dir | more as an example.
Why it matters: Connecting commands lets one command work directly with the results of another.
Key terms: pipeline, pipe, output, |
6.2 Exporting to a file
Introduces exporting PowerShell command output to files so it can be used beyond the screen.
- Purpose of exporting command output: Explain why you might export PowerShell output instead of only viewing it on screen, using Get-Process and CSV as an example.
Why it matters: Exporting lets you do more with command results than read them in the shell.
Key terms: export, CSV, Get-Process
6.2.1 Exporting to CSV
This section shows how to pipe process information to a CSV file, inspect what the file contains, and import it later as a snapshot.
- Exporting pipeline output to CSV: Explain how piping Get-Process to Export-CSV creates a CSV file and how the output filename is supplied.
Why it matters: You can save command results in a file that others can use. - CSV contents versus screen display: Describe the rows and headings in the exported CSV and explain why it may contain more information than the screen shows.
Why it matters: A sparse-looking screen display does not mean the exported CSV is missing that information. - Imported CSV as process snapshot: Explain what a colleague sees after importing an exported process CSV and why it is not live information.
Why it matters: A shared CSV preserves what the process information looked like when it was exported.
Key terms: Get-Process, Export-CSV, pipeline, mandatory positional parameter, CSV file, column headings, onscreen display, import, snapshot, live information
6.2.2 Exporting to JSON
This section explains why JSON is useful for exporting nested process thread information and how PowerShell converts that information to and from JSON.
- Nested process thread exports: Explain why JSON is a better choice than CSV when exporting a process’s nested Threads property.
Why it matters: Choosing JSON preserves thread details that the CSV conversion does not show. - JSON conversion and retrieval: Describe the roles of ConvertTo-Json, Out-File, and ConvertFrom-Json in saving and getting process data back.
Why it matters: It distinguishes converting data from saving it and sets expectations when reading it back.
Key terms: nested property, Threads property, ConvertTo-CSV, ConvertTo-Json, ConvertFrom-Json, Out-File, string on the pipeline
6.2.3 Exporting to XML
This section explains how PowerShell saves and reloads objects with CLIXML, when that format is useful, and how to find related commands.
- CLIXML object round-trip: Explain that Export-Clixml writes a PowerShell-oriented XML file and Import-Clixml reads it back, allowing PowerShell to reconstruct the original objects or something very close. State that the commands require a filename.
Why it matters: CLIXML is useful when the saved results will be used again in PowerShell as objects. - CLIXML format trade-offs: Justify why CLIXML is not always the best export format: its files are often larger, its PowerShell-specific content can be hard for other languages to read, and Windows encrypts security-related portions so only the creating user or machine can decrypt them.
Why it matters: These trade-offs help you decide whether preserving PowerShell objects is worth using CLIXML. - Import and export command discovery: Describe how to look for other import or export cmdlets using Get-Command with the -Verb parameter set to Import or Export.
Why it matters: This gives you a way to discover other file import and export options available in your shell.
Key terms: Export-Clixml, Import-Clixml, CLI XML, objects, Clixml, PowerShell, security-related portions, Get-Command, -Verb, Import
6.2.4 Out-File
This subsection introduces Out-File as a way to write pipeline data to a flat file.
- Out-File pipeline output: Explain that Out-File takes data from the pipeline and writes it to a flat file, such as a text file.
Why it matters: It gives you a way to save pipeline output in a flat file.
Key terms: Out-File, pipeline, flat file
6.2.5 Comparing files
The section shows how to compare a saved PowerShell snapshot with a system’s current state and interpret the differences.
- Snapshot comparison workflow: Describe how to save a reference snapshot in CLIXML, then compare its imported data with current Get-Process output using Compare-Object. Explain why the saved snapshot can serve as a configuration baseline.
Why it matters: A saved baseline lets you check what has changed on a computer without comparing two tables by eye. - Process comparison results: Explain why the example compares process names rather than complete process data, and interpret the indicators in Compare-Object’s output.
Why it matters: Choosing a meaningful comparison property makes the report reflect missing or additional processes rather than routine changes in resource usage. - Comparison file choices: Distinguish why the section favors CLIXML over CSV for these snapshots and why it cautions against using Compare-Object for text-file comparison.
Why it matters: Choosing an appropriate snapshot format and comparison task helps avoid misleading expectations of the command.
Key terms: Compare-Object, CLIXML, Import-Clixml, -ReferenceObject, -DifferenceObject, configuration baselines, -Property, Name, <=, =>
6.3 Piping to a file
This section explains how PowerShell sends formatted output to a file or the screen and shows how to find other Out- cmdlets.
- File output and width: Explain that > is a shortcut for sending output to a file through Out-File, and describe why running Out-File explicitly can be useful.
Why it matters: It helps you choose how to save output and recognize why a file may look different from the screen. - Default screen output path: Trace where output goes when no other Out- cmdlet is specified.
Why it matters: It clarifies what happens to command output before you choose a different destination. - Finding other Out cmdlets: Describe the help and command searches the section suggests for finding other Out- cmdlets.
Why it matters: These searches let you investigate output commands without memorizing their names.
Key terms: Out-File, >, character encodings, 80 columns, Out-Default, Out-Host, host, Help Out*, Get-Command Out*, Get-Command -Verb Out
6.4 Converting to HTML
This section shows how to turn command output into HTML and explains why converting data and saving it are separate pipeline steps.
- HTML conversion and saving: Describe how piping command output to ConvertTo-Html produces browser-readable HTML, and how piping that result to Out-File saves it as a text file.
Why it matters: Separating conversion from saving lets you build a command line one step at a time. - ConvertTo and Export distinction: Distinguish ConvertTo- cmdlets from Export cmdlets, and explain why a conversion-only command can be useful even when an Export command is shorter for saving a file.
Why it matters: The distinction helps you choose whether to save converted data immediately or send it elsewhere.
Key terms: ConvertTo-Html, Out-File, pipeline, Export, ConvertTo-, ConvertTo-CSV, ConvertTo-Xml, Export-CSV, Export-Clixml
6.5 Using cmdlets that modify the system: Killing processes
This section shows how pipelines can feed system-modifying cmdlets and how confirmation and WhatIf help prevent unintended changes.
- Pipelines into modifying cmdlets: Explain how output from Get-Process can be passed to Stop-Process, and why passing every process is dangerous.
Why it matters: A pipeline can turn a command that finds things into one that changes them. - Automatic and requested confirmation: Describe when the shell automatically asks for confirmation and how to request a prompt with -Confirm.
Why it matters: Confirmation gives you a chance to catch an unintended system change. - Previewing changes with WhatIf: Explain what -WhatIf does when used with a cmdlet that could change the system.
Why it matters: WhatIf lets you check a potentially dangerous action before carrying it out.
Key terms: Get-Process, Stop-Process, pipeline, impact level, $ConfirmPreference, -Confirm, -WhatIf
6.6 Common points of confusion
This section explains why files created by Export-CSV or Export-Clixml are usually best read with their matching Import commands rather than as raw text.
- Raw text versus imported data: Distinguish reading an exported file with Get-Content from reading it with the matching Import cmdlet. Explain when to use each approach for CSV or XML files.
Why it matters: Choosing the matching Import cmdlet lets PowerShell interpret exported data instead of leaving you to work with raw text.
Key terms: Export-CSV, Export-Clixml, Get-Content, Import-CSV, Import-Clixml, -IncludeTypeInformation
6.7 Lab
6.8 Lab answers
Where this is used later
- How PowerShell passes data down the pipeline (chapter 10) · The deeper explanation of pipeline data passing builds on this basic connection between commands.
- Working with the default formatting (chapter 11) · Revisits how default formatting affects what appears onscreen.
- What are objects? (chapter 8) · The discussion of objects gives more depth to what CLIXML reconstructs.
- Extensions: Finding and installing modules (chapter 7) · New modules can add commands that can be found through command discovery.
- Parenthetical commands (chapter 10) · The section’s parenthesized commands provide an example of controlling execution order.
- Going out: To a file or to the host (chapter 11) · Distinguishing output sent to a file from output sent to the host.
- Using object actions, or methods (chapter 8) · Uses cmdlets to terminate processes.
Concepts explained in this chapter
- Export-CSVExport-CSV is a PowerShell cmdlet that converts objects to comma-separated values and writes them to a file.
- Export-ClixmlExport-Clixml is a PowerShell cmdlet that converts objects to PowerShell XML representation and saves the result to a file.
- Out-FileOut-File is a PowerShell cmdlet that writes command output to a file.
- background processingA job is a PowerShell-managed background operation that can run asynchronously and be monitored or controlled separately from the interactive pipeline.
- PowerShellPowerShell 3 is the third major version of the PowerShell command-line shell.
- PowerShell hostA PowerShell host is an interface environment or application that receives PowerShell output and provides interaction with a PowerShell session.
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.
