PowerShell Loops ForEach() Method


Example

4.0

Instead of the ForEach-Object cmdlet, the here is also the possibility to use a ForEach method directly on object arrays like so

(1..10).ForEach({$_ * $_})

or - if desired - the parentheses around the script block can be omitted

(1..10).ForEach{$_ * $_}  

Both will result in the output below

1
4
9
16
25
36
49
64
81
100