A Series of Items

Iterating Through an Array

To assist you with iteration, PowerShell provides a command let named ForEach-Object. The formula to use it is:

list | ForEach-Object {
    statement-1
    statement-2
    . . .
    statement-x
}

Here is an example:

$numbers = @(102, 44, 525, 38, 6, 28, 24481, 327, 632, 104)

$numbers | ForEach-Object {
    Write-Host "Number:" $_
}

Write-Host '==============================='
$numbers = @(102, 44, 525, 38, 6, 28, 24481, 327, 632, 104)

$numbers | foreach-object {
    Write-Host "Number:" $PSItem
}

Write-Host '==============================='

Previous Copyright © 2001-2025, FunctionX Wednesday 12 February 2025, 16:36 Next