Difference between revisions of "Foreach-object"

From MyWiki
Jump to: navigation, search
 
(One intermediate revision by the same user not shown)
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.
 
//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:
 
For example, if you have:

Latest revision as of 15:46, 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.
// 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"
 
}