liveBook · Manning

Chapter 23: Adding logic and loops

What you will learn in chapter 23 of Learn PowerShell in a Month of Lunches: Fourth Edition, Covers Windows, Linux, and macOS

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

What it builds on

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.

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.

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.

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.

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.

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.

Key terms: Do While loop, script block, condition

23.4 Lab

23.5 Lab answers

Where this is used later

Concepts explained in this chapter

← Chapter 22: Using someone else’s script Chapter 24: Handling errors →

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.