# Loops

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

Updated: November 10, 2026

## [Repeating](https://www.ask-codey.com/courses/powershell-in-depth/topics/powershell-repeating)

1. [For Each Thing: \`foreach\`](https://www.ask-codey.com/lessons/powershell-foreach-loop) (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\`](https://www.ask-codey.com/lessons/powershell-while-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\`](https://www.ask-codey.com/lessons/powershell-loop-control) (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](https://www.ask-codey.com/courses/powershell-in-depth/topics/exercise-powershell-rename-the-telemetry)

1. [Exercise: Rename the Telemetry](https://www.ask-codey.com/lessons/exercise-powershell-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.

Source: https://www.ask-codey.com/courses/powershell-in-depth/chapters/powershell-loops
