Loops

Do the same thing to every file, or until something is true, and stop early when you need to.

Updated

Repeating

  1. For Each Thing: `foreach` (Apprentice): The foreach statement runs the steps in its braces once for each item in a list, with a variable you name holding this item, so a script renames 30 files or 200 with one loop and can decide about each one with an if.
  2. Until Something Is True: `while`, `do` and `for` (Apprentice): while repeats steps as long as a condition is true, do runs them at least once and checks at the end, which suits trying again up to three times, and for counts from a start to a finish one step at a time.
  3. Stopping Early: `break` and `continue` (Apprentice): A loop keeps a count or a running total in a variable started before it, continue skips the rest of one pass and goes on with the next item, break leaves the loop at once, and inside ForEach-Object return skips an item instead.

Exercise: Rename the Telemetry

  1. Exercise: Rename the Telemetry (Apprentice): Write three small tools for Mission Control, a script that renames every .tmp file in a folder, one that pulls the warnings out of a flight log and a launch countdown, then run them on the real files while a Pester check tells you what's done.