PowerShell gotcha – foreach keyword vs. foreach-object cmdlet

This came up on an internal mailing list and I thought others may find it interesting.  Here was my response.

You’re confusing the foreach keyword with the foreach-object cmdlet – Bruce’s book does an excellent job discussing the differences.

“continue” in your case is looking up the stack for a loop to exit and doesn’t find one, so it exits fully.


C:\Users\jmanning\Documents\bin\tfs # 1,2,3 | foreach-object { if ($_ -eq 2) { continue } ; $_ }


C:\Users\jmanning\Documents\bin\tfs # foreach ($i in 1,2,3) { if ($i -eq 2) { continue } ; $i }



Comments (1)
  1. Buck Hodges says:

    That’s just not right.  It’s a shame they made it sound like a loop.


Comments are closed.

Skip to main content