Introduction
A ForEach loop helps you repeat a set of actions for each item in a list or collection. It saves time by automating repetitive tasks.
foreach ($item in $collection) { # commands to run for each $item }
foreach ($number in 1..5) { Write-Output $number }
foreach ($name in @('Anna', 'Bob', 'Cara')) { Write-Output "Hello, $name!" }
foreach ($file in Get-ChildItem -Path .) {
Write-Output $file.Name
}foreach ($fruit in @('Apple', 'Banana', 'Cherry')) { Write-Output "I like $fruit" }
foreach loop do in PowerShell?foreach ($num in 1..3) { Write-Output ($num * 2) }foreach $item in $array { Write-Output $item }$files. You want to print only those filenames that end with '.txt' using a foreach loop. Which script correctly does this?Pass the test and this pattern moves unknown