Chapter 10: The pipeline, deeper
This introduction illustrates how a PowerShell pipeline can accomplish a multi-step task in one line and previews a deeper look at passing data between commands.
After this chapter you will be able to
- One-line pipeline efficiency. Explain how the example pipeline combines several commands to accomplish in one line what previously took several lines of script.
- Pipeline efficiency without scripts. Explain why the authors recommend learning to manipulate the PowerShell pipeline: it can make powerful one-line commands possible and reduce the need to write complex scripts or programs.
- Pipeline parameter binding. Explain that when Command A sends output to Command B, PowerShell must bind that output to a parameter of Command B. State that it tries ByValue first and, if that fails, ByPropertyName.
- Checking ByValue compatibility. Describe how to check whether one command’s output can bind to another command’s parameter ByValue: inspect the output type with Get-Member, then check the receiving command’s full help for a parameter that accepts that type from the pipeline.
- Useful and misleading String matches. Explain why strings from Get-Content bind to the -Name parameter of both Get-Command and Get-Module, but only the Get-Module match serves the example’s purpose of finding modules.
- ByPropertyName parameter matching. Explain how PowerShell matches properties on incoming objects to parameters of the next command, and how to check whether those parameters accept pipeline input ByPropertyName.
- CSV driven alias creation. Trace how importing the example CSV supplies Name and Value properties to New-Alias and creates three aliases.
- CSV Property Name Mismatches. Explain why the HR-provided CSV objects cannot be passed directly to New-ADUser by property name, despite importing successfully.
- Pipeline Custom Property Construction. Describe how Select-Object creates properties on the piped CSV objects and why doing so avoids repeatedly editing the source file.
- Azure PowerShell connection setup. Describe the steps to get Azure PowerShell ready: obtain an Azure subscription, install the Az module, run Connect-AzAccount, and follow its sign-in instructions.
- Parenthetical parameter input. Explain why piping module names from Get-Content to Get-Command does not populate -Module, and how putting the producing command in parentheses supplies its results directly to that parameter. State that the results must be the type the parameter expects.
- Matching resource group input types. Explain why passing a Get-AzResourceGroup result directly to New-AzStorageAccount’s -ResourceGroupName parameter fails, and how extracting ResourceGroupName fixes it.
What it builds on
- Connecting one command to another: Less work for you (chapter 6) · Connecting one command to another through a pipeline.
- Discovering objects: Get-Member (chapter 8) · Using Get-Member to inspect command output.
- Interpreting the help (chapter 3) · Reading parameter information in command help.
- Selecting the properties you want (chapter 8) · Selecting object properties with Select-Object.
- Extensions: Finding and installing modules (chapter 7) · Installing a PowerShell module
Section by section
Introduction
This introduction illustrates how a PowerShell pipeline can accomplish a multi-step task in one line and previews a deeper look at passing data between commands.
- One-line pipeline efficiency: Explain how the example pipeline combines several commands to accomplish in one line what previously took several lines of script.
Why it matters: This is why connecting commands is useful before you learn more about the pipeline.
Key terms: pipeline, Get-Process, Sort-Object, ConvertTo-Html, Out-File
10.1 The pipeline: Enabling power with less typing
This section explains why learning to use PowerShell’s pipeline can make administrators more effective with less typing and less need for scripts.
- Pipeline efficiency without scripts: Explain why the authors recommend learning to manipulate the PowerShell pipeline: it can make powerful one-line commands possible and reduce the need to write complex scripts or programs.
Why it matters: It explains the practical payoff for learning more about the pipeline.
Key terms: PowerShell pipeline, one-line commands, scripts
10.2 How PowerShell passes data down the pipeline
The section explains how PowerShell connects one command’s output to another command’s input through pipeline parameter binding.
- Pipeline parameter binding: Explain that when Command A sends output to Command B, PowerShell must bind that output to a parameter of Command B. State that it tries ByValue first and, if that fails, ByPropertyName.
Why it matters: It explains why passing output into a pipeline command depends on that command’s parameters.
Key terms: pipeline parameter binding, ByValue, ByPropertyName, Command A, Command B
10.3 Plan A: Pipeline input ByValue
Explains how PowerShell binds pipeline objects to parameters ByValue, how to check whether a match is useful, and when ByValue fails.
- Checking ByValue compatibility: Describe how to check whether one command’s output can bind to another command’s parameter ByValue: inspect the output type with Get-Member, then check the receiving command’s full help for a parameter that accepts that type from the pipeline.
Why it matters: This check lets you predict what a pipeline will do instead of relying on how the command looks. - Useful and misleading String matches: Explain why strings from Get-Content bind to the -Name parameter of both Get-Command and Get-Module, but only the Get-Module match serves the example’s purpose of finding modules.
Why it matters: You must check what a matching parameter means, not just whether it accepts the object type. - FileInfo ByValue failure: Explain why output from Get-ChildItem cannot bind ByValue to Stop-Process and state what PowerShell tries next.
Why it matters: It shows that an unsuccessful type match leads PowerShell to try another binding approach.
Key terms: pipeline parameter binding, ByValue, Get-Member, Get-Help, type of object, Get-Content, System.String, Get-Command, Get-Module, -Name
10.4 Plan B: Pipeline input ByPropertyName
This section explains how PowerShell connects pipeline object properties to accepting parameters by name, then shows why matching names alone may not produce a useful result.
- ByPropertyName parameter matching: Explain how PowerShell matches properties on incoming objects to parameters of the next command, and how to check whether those parameters accept pipeline input ByPropertyName.
Why it matters: This lets you predict which values a downstream command will receive without typing each parameter explicitly. - Matching names versus usable values: Explain why piping Get-ChildItem output to Stop-Process produces errors even though its Name property connects to the -Name parameter.
Why it matters: It helps distinguish a successful pipeline connection from a command that can use the supplied data. - CSV driven alias creation: Trace how importing the example CSV supplies Name and Value properties to New-Alias and creates three aliases.
Why it matters: The example shows how prepared data can drive a command with little repeated typing.
Key terms: pipeline input ByPropertyName, property, parameter, Get-Member, Get-ChildItem, Stop-Process, Name, -Name, Import-Csv, New-Alias
10.5 When things don’t line up: Custom properties
Shows how to reshape imported CSV objects with custom properties so their names match a cmdlet’s ByPropertyName pipeline parameters.
- CSV Property Name Mismatches: Explain why the HR-provided CSV objects cannot be passed directly to New-ADUser by property name, despite importing successfully.
Why it matters: A CSV can import without errors yet still have the wrong shape for the next command. - Pipeline Custom Property Construction: Describe how Select-Object creates properties on the piped CSV objects and why doing so avoids repeatedly editing the source file.
Why it matters: You can adapt recurring, imperfect input in the pipeline instead of manually fixing every CSV file.
Key terms: ByPropertyName, Import-CSV, New-ADUser, Department, samAccountName, Name, Select-Object, -Property, hash table, Expression
10.6 Working with Azure PowerShell
This section shows how to prepare and connect Azure PowerShell for the rest of the chapter.
- Azure PowerShell connection setup: Describe the steps to get Azure PowerShell ready: obtain an Azure subscription, install the Az module, run Connect-AzAccount, and follow its sign-in instructions.
Why it matters: These steps prepare your shell to run the Azure examples that follow. - Correct subscription selection: Explain why a reader with multiple Azure subscriptions may need to select one explicitly, and give the section’s Select-AzSubscription example.
Why it matters: Checking the selected subscription helps ensure subsequent commands use the one you intended.
Key terms: Azure subscription, Az module, Connect-AzAccount, Select-AzSubscription, SubscriptionName
10.7 Parenthetical commands
This section shows how a parenthetical command can supply values directly to a parameter when pipeline binding does not send them where intended.
- Parenthetical parameter input: Explain why piping module names from Get-Content to Get-Command does not populate -Module, and how putting the producing command in parentheses supplies its results directly to that parameter. State that the results must be the type the parameter expects.
Why it matters: Parentheses give you a way to supply a parameter with values when piping them binds to the wrong parameter.
Key terms: parenthetical command, pipeline parameter binding, -Module, -Name, String objects
10.8 Extracting the value from a single property
This section shows how to extract a property’s value so a parenthetical command can supply the string another cmdlet expects.
- Matching resource group input types: Explain why passing a Get-AzResourceGroup result directly to New-AzStorageAccount’s -ResourceGroupName parameter fails, and how extracting ResourceGroupName fixes it.
Why it matters: Extracting the value lets you connect commands whose output objects and input parameters do not otherwise match. - Property selection versus expansion: Distinguish Select-Object -Property from -ExpandProperty when supplying a property value to another command.
Why it matters: The distinction explains why selecting the right property can still leave you with the wrong kind of input.
Key terms: PSResourceGroup, String, ResourceGroupName, Select-Object, -ExpandProperty, -Property, PSCustomObject
10.9 Lab
10.10 Lab answers
10.11 Further exploration
This section encourages readers to revisit the pipeline examples and compare two commands’ output, without explaining the difference.
Where this is used later
- The backup plan: Enumerating objects (chapter 15) · Introduces pipeline input ByValue.
- Creating custom columns and list entries (chapter 11) · Introduces hash table constructs for custom properties.
- When $_ is allowed (chapter 12) · Parenthetical commands supplying a result to another command
- Using Invoke-Command for one-to-many remoting (chapter 13) · Introduces using parentheses to run a command before supplying its result elsewhere.
Concepts explained in this chapter
- pipeline parameter binding bypropertynamePipeline parameter binding byPropertyName is PowerShell's mechanism for binding a pipeline input object's property value to a command parameter with a matching name.
- parameter bindingPipeline parameter binding byValue is PowerShell's mechanism for binding a pipeline input object to a command parameter based on the input object's type.
- Select-ObjectSelect-Object is a PowerShell cmdlet that selects specified properties or creates customized objects from input objects.
- -expandproperty-ExpandProperty is a Select-Object parameter that outputs the values of a specified property instead of objects containing that property.
- script blockA script block is a PowerShell language construct that groups executable statements inside braces for invocation or deferred execution.
- custom objectPSCustomObject is a PowerShell wrapper object type that exposes a selected set of properties.
- stringA string, represented by the .NET System.String type, is a sequence of characters representing text data.
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.
