foreach Loop
noun
say it: “for-EECH LOOP” /fɔrˈitʃ ˈlup/
A loop that runs the same steps once for each item in a list, such as every file in a folder.
What is a foreach loop in PowerShell?
A foreach loop runs the steps in its braces once for each item in a list. The variable you name before in holds this item on each pass. Use it in a script; in a pipeline, use ForEach-Object.
foreach ($system in 'fuel', 'power', 'radio') {
"Checking $system... go"
}
# Checking fuel... go
# Checking power... go
# Checking radio... go
In a sentence
- “A foreach loop renamed all thirty telemetry files from .tmp to .log.”
- “Inside the foreach loop, $sensor holds one sensor's file on each pass.”
Level: Apprentice · More PowerShell terms at this level
Updated