It works as expected, breaking out of the foreach loop and continuing the function. I can even be specific and assign the loop a label, and the break specifically from that label. But if I shorthand it:
The break not only takes me out of the loop, but out of the function and the whole script. I cannot assign a label to the shorthand version of the loop statement, and have yet to find a way to break only out of the foreach loop in this case. Yes, I know I should write things out rather than shorthand, I am just curious if this is intended behavior or not.
Actually, that is not a short hand. Those two are two different items. ForEach-Object is a cmdlet and foreach is a looping construct, hence they behaves differently.
While foreach is a loop (similar to foreach in c#), ForEach-Object is a cmdlet and when used in pipeline it takes objects coming throught the pipeline one at a time and its not same as looping, but similar.
Thanks for the responses, I guess I didn’t realize the difference between just the foreach loop and the foreach-object cmdlet. I did try both return (which doesn’t break out of the loop) and exit (tries to exit the entire session) but neither seem to do the trick. As I said, I will stick with the long version, was just curious at the apparent difference in behavior.