Chapter 15: Working with many objects, one at a time
This introduction frames PowerShell administration as work that often needs to be repeated across multiple targets.
After this chapter you will be able to
- Multiple-target administration. Explain why automating administration often involves performing tasks across multiple targets, using an example from the section.
- Batch cmdlet processing. Explain how a cmdlet can accept objects from the pipeline and perform an action on each, making batch management possible with simple syntax.
- Batch CIM method invocation. Explain why configuration objects cannot be piped directly to EnableDHCP and how Invoke-CimMethod instead applies that method to a batch of CIM objects. Describe the same-type constraint and the section's distinction between invoking through the cmdlet and calling a method directly.
- One-at-a-time fallback. Explain when to fall back to enumerating objects and what that approach does.
- One-at-a-time vault creation. Explain why the example uses ForEach-Object instead of continuing to try to make New-AzKeyVault handle the whole list.
- ForEach-Object pipeline mechanics. Trace how Get-Content, ForEach-Object, the -Process script block, and $_ work together in the vault example.
- Parallel versus sequential processing. Explain how ForEach-Object -Parallel differs from regular ForEach-Object in the timed example.
- Parallel throttle limit. Describe the default limit on concurrently running script blocks and how changing it affects the ten-line example.
- Choosing an action approach. Explain what a batch or action cmdlet does and why the authors prefer one when available. Describe how other approaches can produce the same result and why the choice may vary by task.
- Parallel processing diminishing returns. Explain why setting ThrottleLimit to the number of tasks does not guarantee the fastest completion, using the section’s 100-task example.
- Finding object method documentation. Explain how to discover an object's methods with Get-Member, then use its TypeName to find documentation for a specific method.
- Per-object script block context. Explain that ForEach-Object runs its script block once for each piped object, and that $_ refers to the current object. Distinguish using $_ for the whole object from using dot notation to access one of its properties or methods.
What it builds on
- Connecting one command to another: Less work for you (chapter 6) · Objects can be piped from one command to another.
- Using object actions, or methods (chapter 8) · Methods as actions attached to objects
- Filtering left (chapter 12) · Filtering results to the intended targets
- Plan A: Pipeline input ByValue (chapter 10) · Introduces pipeline input ByValue.
- Plan B: Pipeline input ByPropertyName (chapter 10) · Introduces pipeline input ByPropertyName.
- When $_ is allowed (chapter 12) · Assumes prior exposure to the $_ placeholder.
- Creating a thread job (chapter 14) · Introduces thread jobs, which the section compares with parallel work.
- Discovering objects: Get-Member (chapter 8) · Using Get-Member to inspect objects.
Section by section
Introduction
This introduction frames PowerShell administration as work that often needs to be repeated across multiple targets.
- Multiple-target administration: Explain why automating administration often involves performing tasks across multiple targets, using an example from the section.
Why it matters: Recognizing the multiple-target task helps explain why this chapter looks beyond acting on one object at a time.
Key terms: automate administration, multiple targets
15.1 The preferred way: “Batch” cmdlets
The section presents cmdlets that act on batches of objects as the preferred way to perform repeated tasks, and shows how -PassThru makes their results visible.
- Batch cmdlet processing: Explain how a cmdlet can accept objects from the pipeline and perform an action on each, making batch management possible with simple syntax.
Why it matters: A suitable cmdlet can do the per-object work without requiring you to handle each item yourself. - Visible results with PassThru: Describe why an action cmdlet may show no output and how -PassThru changes what appears after it runs.
Why it matters: No output need not mean the action failed; -PassThru can make the affected objects visible.
Key terms: batch cmdlets, pipeline, objects, -PassThru, Set-Service, Copy-Item
15.2 The CIM way: Invoking methods
Shows how to find and invoke a CIM object's method across multiple Windows network adapter configurations, then interpret the results.
- CIM target and method discovery: Describe how the section identifies Intel network adapter configurations for a DHCP change and finds the EnableDHCP method when no suitable batch cmdlet exists. State that this CIM example is Windows-only and that PowerShell 7 requires CIM rather than WMI commands.
Why it matters: Finding the right objects and their available methods comes before trying to change them. - Batch CIM method invocation: Explain why configuration objects cannot be piped directly to EnableDHCP and how Invoke-CimMethod instead applies that method to a batch of CIM objects. Describe the same-type constraint and the section's distinction between invoking through the cmdlet and calling a method directly.
Why it matters: This lets you act on many CIM objects without mistaking an object's method for a command in the pipeline. - CIM method result interpretation: Describe what Invoke-CimMethod reports and how the example determines what its return values mean. Explain the limitation in matching each result to a particular adapter configuration.
Why it matters: A return number is useful only if you look up its meaning and recognize what the output does not identify.
Key terms: CIM, Win32_NetworkAdapterConfiguration, Get-CimClass, CimClassMethods, EnableDHCP, Invoke-CimMethod, CIM objects, -WhatIf, -Confirm, ReturnValue
15.3 The backup plan: Enumerating objects
This section explains when enumerating objects becomes a fallback and why a cmdlet’s pipeline-input rules can make that fallback necessary.
- One-at-a-time fallback: Explain when to fall back to enumerating objects and what that approach does.
Why it matters: It gives you a way forward when you cannot express the task as a direct pipeline between cmdlets. - Pipeline input binding limits: Distinguish the ByPropertyName input described here from the ByValue input the example needs.
Why it matters: Checking how a parameter accepts pipeline input helps explain why piping a list of values may fail.
Key terms: batch cmdlet, enumerate the objects, scripting construct, ByPropertyName, ByValue, Id, Name parameter
15.3.1 Making the cmdlets work for you
This subsection shows how to create multiple vaults by feeding names from a text file to a cmdlet that accepts one name at a time.
- One-at-a-time vault creation: Explain why the example uses ForEach-Object instead of continuing to try to make New-AzKeyVault handle the whole list.
Why it matters: This approach lets you use a single-item cmdlet for a list of names without assuming it supports batch input. - ForEach-Object pipeline mechanics: Trace how Get-Content, ForEach-Object, the -Process script block, and $_ work together in the vault example.
Why it matters: Following the current value through the pipeline makes the repeated command easier to read and adapt.
Key terms: New-AzKeyVault, ForEach-Object, enumerate, Get-Content, -Process, script block, $_, Name parameter
15.4 Let’s speed things up
This section shows how PowerShell 7’s ForEach-Object -Parallel can shorten work on multiple items and how its throttle limit affects the result.
- Parallel versus sequential processing: Explain how ForEach-Object -Parallel differs from regular ForEach-Object in the timed example.
Why it matters: Concurrent work can save time when independent items each take a while to process. - Parallel throttle limit: Describe the default limit on concurrently running script blocks and how changing it affects the ten-line example.
Why it matters: The default concurrency limit explains why adding more items does not always leave the total time unchanged.
Key terms: ForEach-Object, -Parallel, Measure-Command, script blocks, throttle limit, ThrottleLimit, Parallel ForEach
15.5 Common points of confusion
This section introduces common sources of confusion in the chapter and points readers to the explanations that follow.
15.5.1 Which way is the right way?
This section compares ways to act on a group of PowerShell objects and explains why no single approach is always right.
- Choosing an action approach: Explain what a batch or action cmdlet does and why the authors prefer one when available. Describe how other approaches can produce the same result and why the choice may vary by task.
Why it matters: You can choose a workable approach instead of searching for a single mandatory way to handle many objects.
Key terms: batch cmdlet, action cmdlet, ForEach-Object, -Parallel
15.5.2 Diminishing returns of Parallel ForEach
This subsection explains why increasing Parallel ForEach’s ThrottleLimit can make work slower rather than faster.
- Parallel processing diminishing returns: Explain why setting ThrottleLimit to the number of tasks does not guarantee the fastest completion, using the section’s 100-task example.
Why it matters: It helps you avoid assuming that running every task at once is the quickest choice.
Key terms: Parallel ForEach, ThrottleLimit, context switch, diminishing returns
15.5.3 Method documentation
This subsection explains how to find documentation for an object's methods when PowerShell help does not provide it.
- Finding object method documentation: Explain how to discover an object's methods with Get-Member, then use its TypeName to find documentation for a specific method.
Why it matters: This gives you a way to find out what a method does after you discover its name.
Key terms: Get-Member, TypeName, methods
15.5.4 ForEach-Object confusion
This subsection clarifies how to read and write ForEach-Object commands that operate on each piped object, especially when calling methods.
- Per-object script block context: Explain that ForEach-Object runs its script block once for each piped object, and that $_ refers to the current object. Distinguish using $_ for the whole object from using dot notation to access one of its properties or methods.
Why it matters: This lets you tell which object each operation in a ForEach-Object command acts on. - Readable method-call syntax: Describe how to make a ForEach-Object command easier to read by spelling out its cmdlet name, and state how method calls are written after $_.
Why it matters: Spelling out the cmdlet and recognizing method-call punctuation makes dense command lines easier to follow.
Key terms: ForEach-Object, script block, $_, pipeline, properties, methods, %, ForEach, method, parameters
15.6 Lab
15.7 Lab answers
Where this is used later
- Foreach-Object (chapter 23) · Provides an earlier example of ForEach-Object before its later treatment.
- Foreach-Object -Parallel (chapter 23) · Provides the basic distinction between sequential and parallel ForEach-Object processing.
Concepts explained in this chapter
- Common Information ModelCIM, the Common Information Model, is a standard information model for representing managed computing resources and their relationships.
- Invoke-CimMethodInvoke-CimMethod is a PowerShell cmdlet that invokes a method on one or more CIM objects.
- Win32_NetworkAdapterConfigurationWin32_NetworkAdapterConfiguration is a Windows CIM class representing the configuration of a network adapter.
- Windows Management InstrumentationWMI is Windows Management Instrumentation, a Windows management technology for querying and administering system resources.
- batch cmdletA batch cmdlet is a PowerShell cmdlet technique for applying an operation to multiple targets in a single command invocation.
- script blockA script block is a PowerShell language construct that groups executable statements inside braces for invocation or deferred execution.
- -parallel-Parallel is a ForEach-Object parameter that runs the supplied script block concurrently for multiple input objects.
- 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.
- 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.
- methodA method is a callable operation associated with an object that can direct the object to perform an action.
- Get-MemberGet-Member is a PowerShell cmdlet that displays the members, including properties and methods, of input objects.
- type nameA typename is the unique name identifying an object's type.
- aliasA parameter alias is an alternate name accepted by a command for one of its parameters.
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.
