Foreach-object

From MyWiki
Revision as of 15:46, 8 December 2014 by George2 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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"
 
}