Exit From Foreach Object in PowerShell

Rohan Timalsina Jan 31, 2022
  1. Use the break Condition to Exit From ForEach-Object in PowerShell
  2. Use the if to Exit From ForEach-Object in PowerShell
Exit From Foreach Object in PowerShell

The ForEach-Object cmdlet allows the user to iterate through collections and operate against each item in a collection of input objects. In ForEach-Object, the input objects are piped to the cmdlet or specified using the -InputObject parameter.

There are two different ways of constructing a ForEach-Object command in PowerShell: Script block and Operation statement. The ForEach-Object cmdlet runs each input object’s script block or operation statement.

Use a script block to specify the operation. The $_ variable is used within the script block to represent the current input object.

The script block can contain any PowerShell script. For example, the following command gets the value of each cmdlet’s Name property, function, and aliases installed on the computer.

Get-Command | ForEach-Object {$_.Name}

Another way to construct the ForEach-Object command is to use the operation statement. You can specify a property value or call a method with the operation statement.

Get-Command | ForEach-Object Name

Sometimes, there are situations when you might want to exit from the ForEach-Object, but it works differently than the ForEach statement. In ForEach-Object, the statement is executed as soon as each object is produced.

In the ForEach statement, all the objects are collected before the loop executes. The ForEach-Object is a cmdlet, not an actual loop.

When you use break or continue to exit the loop, the whole script is terminated instead of skipping the statement after it. However, it is possible to exit from the ForEach-Object object using some conditions in PowerShell.

Use the break Condition to Exit From ForEach-Object in PowerShell

We have created a collection of objects $numbers to use as the input object in ForEach-Object.

$numbers = "one","two","three","four","five"

The Where-Object cmdlet allows you to select objects from a collection based on their property values. You can apply the exit logic in Where-Object first and pass objects to ForEach-Object.

Then use the break condition as shown below to exit from ForEach-Object in PowerShell.

$Break = $False;

$numbers | Where-Object { $Break -eq $False } | ForEach-Object {

    $Break = $_ -eq "three";

    Write-Host "The number is $_.";
}

When the object value equals three, ForEach-Object will skip iterating through a collection of objects. As a result, we will exit from ForEach-Object in PowerShell.

Output:

The number is one.
The number is two.
The number is three.

Use the if to Exit From ForEach-Object in PowerShell

In this method, you need to use an empty value in a collection of objects to exit from ForEach-Object in PowerShell. For example, you can use the if condition to exit from ForEach-Object.

$numbers = "one","two","three","","four"

$numbers | ForEach-Object{
        if($_ -eq ""){
        break;
}
    Write-Host "The number is $_."
}

We have created a collection of objects $numbers, which contains the number in letters and has one empty value.

Inside the ForEach-Object, we have created an if condition which states if the object’s value equals null, then break the ForEach-Object statement.

Output:

The number is one.
The number is two.
The number is three.

As we can see, the program’s output is printed up to three. There is a null value in the collection of objects.

The program is terminated after it. In this way, we can achieve the result and exit from the ForEach-Object.

Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

Related Article - PowerShell Object