How do you break out of ForEach-Object loop in powershell????

I was doing some simple looping using ForEach-Object and I wanted to break out of the loop after I found the value I was looking for. Naturally, I did this:

image

Yeah, that didn’t work out so well. That broke out of the entire script I was running. Some quick googling…. don’t use break or continue in a ForEach-Object loop. In my case, just use return.

image

Leave a Reply

Your email address will not be published. Required fields are marked *