Chapter 23: Adding logic and loops
Introduces looping as a way to process objects one at a time or execute a block of code repeatedly in PowerShell.
After this chapter you will be able to
- Purpose of looping. Explain what looping means here and why it is useful in PowerShell.
- Foreach collection iteration. Explain how Foreach works through an array and what happens on each iteration in the example.
- Pipeline processing with ForEach-Object. Trace how Get-ChildItem | ForEach-Object {$_.name} processes its input and produces output.
- Parallel versus sequential execution. Explain how -Parallel changes Foreach-Object from processing input objects sequentially to running the command block for multiple objects at the same time, using the five-item timing example.
- Throttle limit and result order. Describe the default limit on simultaneous script blocks, its effect when the input grows from five to ten items, and what happens to the order of results.
- While loop execution. Explain the syntax of a while loop and how its condition controls repeated execution of commands in its script block.
- Do While execution order. Explain how Do While differs from While: it runs the commands before checking the condition, so they execute at least once even if the condition is initially false.
What it builds on
- Storing many objects in a variable (chapter 16) · Uses the idea of storing multiple objects in a variable.
Section by section
Introduction
Introduces looping as a way to process objects one at a time or execute a block of code repeatedly in PowerShell.
- Purpose of looping: Explain what looping means here and why it is useful in PowerShell.
Why it matters: This gives you the basic reason to use a loop before learning particular PowerShell loops.
Key terms: looping, list of objects, block of code
23.1 Foreach and Foreach-Object
This section introduces a distinction between Foreach and Foreach-Object and points to a figure, but does not yet explain how either works.
23.1.1 Foreach
This section explains how Foreach processes each item in an array using a temporary variable and a script block.
- Foreach collection iteration: Explain how Foreach works through an array and what happens on each iteration in the example.
Why it matters: It lets you predict what a Foreach loop will do with every item in a collection.
Key terms: Foreach, collection, array, process block, script block, $array
23.1.2 Foreach-Object
This subsection shows how ForEach-Object processes pipeline objects one at a time and explains when to choose it over Foreach.
- Pipeline processing with ForEach-Object: Trace how Get-ChildItem | ForEach-Object {$_.name} processes its input and produces output.
Why it matters: It lets you explain what a pipeline command does to each object it receives. - Foreach versus ForEach-Object: Distinguish the section's recommended uses of Foreach and ForEach-Object.
Why it matters: This rule of thumb helps you choose the form that fits how your objects are being supplied. - Sequential item processing: Explain the order in which Foreach and ForEach-Object process items and its possible runtime consequence.
Why it matters: It explains why per-item work can take longer when there is a lot of work or input.
Key terms: ForEach-Object, script block, pipeline, $_, Name property, Foreach, sequentially, input collection, process block
23.1.3 Foreach-Object -Parallel
This section explains how Foreach-Object -Parallel runs work concurrently, how its throttle limit affects timing, and what trade-offs parallel execution brings.
- Parallel versus sequential execution: Explain how -Parallel changes Foreach-Object from processing input objects sequentially to running the command block for multiple objects at the same time, using the five-item timing example.
Why it matters: It explains why -Parallel can shorten a task that repeats work for many input objects. - Throttle limit and result order: Describe the default limit on simultaneous script blocks, its effect when the input grows from five to ten items, and what happens to the order of results.
Why it matters: These limits help explain both the timing and the output you see when running work in parallel. - Runspace performance trade-off: Explain why -Parallel is not necessarily faster when its script blocks are resource intensive.
Why it matters: It cautions against assuming that parallel execution always improves performance.
Key terms: Foreach-Object, -Parallel, sequentially, command block, throttle limit, -throttlelimit, script blocks, runspace, performance degradation, resource intensive
23.2 While
This section explains how a PowerShell while loop runs commands while a Boolean condition remains true.
- While loop execution: Explain the syntax of a while loop and how its condition controls repeated execution of commands in its script block.
Why it matters: The condition determines both whether the commands repeat and when the loop ends. - Compound conditions and nontermination: Describe how logic operators can be used in a while-loop condition and why a loop may need to be interrupted.
Why it matters: A condition that never becomes false can leave the loop running until you interrupt it.
Key terms: while loop, condition, script block, Boolean, -and, -or, condition statement, Ctrl-C
23.3 Do While
This section explains how Do While runs its commands at least once by checking its condition after the commands execute.
- Do While execution order: Explain how Do While differs from While: it runs the commands before checking the condition, so they execute at least once even if the condition is initially false.
Why it matters: Do While is useful when the commands need to run once before you can decide whether to repeat them.
Key terms: Do While loop, script block, condition
23.4 Lab
23.5 Lab answers
Where this is used later
- Handling exceptions (chapter 24) · Using foreach to process items individually.
Concepts explained in this chapter
- 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.
- arrayAn array is an ordered collection of values that can be accessed as a single sequence.
- bindingA variable is a named storage location that holds a value or collection of values and can be assigned a new value.
- collectionA collection is a container that groups multiple values as a single data object.
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.
