u/Bronson_R_9346754

So you can do this;

$mySet = 1..5

$mySet | ForEach-Object {$_}

..and does that simple $_ comand (prints the object)

Can you add multiple commands inside that {} code block ?

Eg

$mySet = 1..5

[int]$total = 0

$mySet | ForEach-Object {$_ , $total = $total + $_ }

(That code doesn't work of course, but I'm wondering if its possible to perform multiple actions inside each iteration of ForEach-Object ?

Any help is appreciated :)

_____________________________________________________________________________________________

UPDATE: Thank you all. This works;
[int]$sum = 0

$numbers = 1..5

$numbers | ForEach-Object {

$sum = $sum + $_

$_

}

Write-Host "Total of all those numbers is $sum"

The output is .......................................

1

2

3

4

5

Total of all those numbers is 15

reddit.com
u/Bronson_R_9346754 — 24 days ago