共用方式為


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 } ; $_ }

1

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

1

3

Comments

  • Anonymous
    March 27, 2007
    That's just not right.  It's a shame they made it sound like a loop. Buck