Foreach-object

From MyWiki
Jump to: navigation, search
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.
// Foreach is different to foreach-object
 
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"
 
}