2

Just noticed this weired beaviour using ForEach-Object (%), the continue statement, which is supposed to skip the current iteration, acts like the break statement and breaks the loop.

See example (should skip number 5):

1..10 | % {if ($_ -eq 5) {continue} else {$_}}

Results:

1
2
3
4

Using a foreach statement is working as expected:

foreach ($n in 1..10) {if ($n -eq 5) {continue} else {$n}}

Results:

1
2
3
4
6
7
8
9
10

Why is that? Is it a bug or does it have a purpose?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
JustCurious
  • 183
  • 1
  • 11

0 Answers0