Difference between revisions of "Foreach-object"
From MyWiki
(Created page with "<source lang="java"> 1..100 | ForEach-Object { if ($_ % 7 -ne 0 ) { return } Write-Host "$($_) is a multiple of 7" } </source>") |
|||
| Line 4: | Line 4: | ||
Write-Host "$($_) is a multiple of 7" | Write-Host "$($_) is a multiple of 7" | ||
} | } | ||
| + | |||
| + | //Because For-Each object is a cmdlet and not a loop and continue / break do not apply to it. | ||
| + | |||
| + | For example, if you have: | ||
| + | |||
| + | $b = 1,2,3 | ||
| + | |||
| + | foreach($a in $b){ | ||
| + | |||
| + | $a | foreach { if($_ -eq 2) {continue;} else {write-host $_} } | ||
| + | |||
| + | write-host "after" | ||
| + | |||
| + | } | ||
| + | |||
</source> | </source> | ||
Revision as of 15:43, 8 December 2014
1..100 | ForEach-Object { if ($_ % 7 -ne 0 ) { return } Write-Host "$($_) is a multiple of 7" } //Because For-Each object is a cmdlet and not a loop and continue / break do not apply to it. For example, if you have: $b = 1,2,3 foreach($a in $b){ $a | foreach { if($_ -eq 2) {continue;} else {write-host $_} } write-host "after" }