PowerShell Break ForEach Loop

PowerShell break foreach loop

Usually, learners get confused with ForEach and ForEach-Object while using break/continue statements in PowerShell, but there is a clear difference between the ForEach loop and ForEach-Object cmdlet, that’s why break/continue statements do not work in ForEach-Object cmdlet as they work in ForEachLoop.

We will learn about both statements in this article, but before diving into details, it is mandatory to note that both statements (break/continue) are used to alter the flow of the loop, but there is a difference between them.

The break statement immediately exits the loop, regardless of whether the specified condition has been satisfied. It means that any remaining iterations of a loop will be skipped, and execution will start from the statement immediately following the loop.

On the other hand, the continue statement does not exit the loop but skips the current iteration of the loop and moves to the next iteration. It means that any code following the continue statement within the current iteration of the loop is skipped, but the loop itself continues executing within the next iteration.

Let’s learn them with code examples below.

Using break/continue Statements in the ForEach Loop

Use the break statement in the ForEach loop in PowerShell.

First, we used the .. operator to create a range of numbers based on the given lower & upper limit and stored it in the $range variable. Next, we used the ForEach loop to iterate over the $range.

Finally, we used the if statement with the -eq operator in each iteration to check if the current number (represented with $number) equals 5. If it is, exit the loop using the break statement; otherwise, print the number using the Write-Host cmdlet. Finally, we used another Write-Host outside the ForEach to show when we exit from the loop.

The above code exited the ForEach loop when the $number was equal to 5. As a result, the Write-Host cmdlet following the break statement would not be executed.

Use the continue statement in the ForEach loop in PowerShell.

This example is the same as the previous one, but we used the continue statement this time. When the if statement is satisfied, the continue statement skips the current iteration and jumps to the next one. Note that any statement(s) following the continue statement will not be executed.

Use break and continue statements in the ForEach loop in PowerShell.

This example is the combination of the last two code examples. It skipped the iteration at number 5 and continued with the subsequent iterations but exited the loop when the number was 9.

The break can also be used within the switch and trap in PowerShell.

Let’s add a twist to the above examples. Suppose a situation where you are using the ForEach loop within the while loop. Now, you have two arrays, each of them containing integer values. The while loop iterates over array1. On the other hand, the ForEach iterates over array2 for every iteration of the while loop. Use the break statement inside the ForEach based on the specified if statement.

The worth noting point is that the break statement will break the ForEach loop as soon as the if statement is satisfied, but your job is to break ForEach and while both loops using one break statement that you are bound to use within the ForEach. How to do it? Let’s learn it in the following example.

Use the Labelled break statement in the ForEach loop

To use labelled break in ForEach in PowerShell:

  • Create two arrays containing integer values and store them in separate variables.
  • Use the while to iterate over the first array.
  • For every iteration of the while loop:
    • Use the ForEach to iterate over the second array.
    • Use the if statement with the -eq operator to determine if a current number of the second array is equal to 8.
    • If it is, break ForEach and while loops; otherwise, print the number and move to the next iteration.
  • We used two Write-Host cmdlets to demonstrate when we leave ForEach and while loops.

Here, we used a labelled break statement, which exits the labelled loop rather than leaving the current loop in PowerShell.

Using break/continue/return Statements in ForEach-Object Cmdlet

Use the break statement in the ForEach-Object cmdlet in PowerShell.

We expected the break statement to exit the ForEach-Object at number 5 and execute the Write-Host outside the ForEach-Object to print the I am out of ForEach_Object now. message, but it cancelled the entire script at number 5.

Use the continue statement in the ForEach-Object cmdlet in PowerShell.

The continue statement also did not work as it should. Instead of skipping the iteration at number 5 and continuing from the next iterations, it cancelled the script at number 5 and did not print the I am out of ForEach_Object now. message.

So, the above two examples demonstrated that break and continue statements can’t be used to alter the flow of ForEach-Object; we need to use the return statement; see the following example.

Use the return statement in the ForEach-Object cmdlet in PowerShell.

See, the return skipped the iteration at number 5 and continued from the next iterations. Remember, using the return statement within the ForEach loop will break the entire script as break and continue statements did with ForEach-Object.

That’s all about PowerShell break ForEach loop.

Was this post helpful?

Leave a Reply

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