How to exit from ForEach-Object in PowerShell How to exit from ForEach-Object in PowerShell powershell powershell

How to exit from ForEach-Object in PowerShell


Item #1. Putting a break within the foreach loop does exit the loop, but it does not stop the pipeline. It sounds like you want something like this:

$todo=$project.PropertyGroup foreach ($thing in $todo){    if ($thing -eq 'some_condition'){        break    }}

Item #2. PowerShell lets you modify an array within a foreach loop over that array, but those changes do not take effect until you exit the loop. Try running the code below for an example.

$a=1,2,3foreach ($value in $a){  Write-Host $value}Write-Host $a

I can't comment on why the authors of PowerShell allowed this, but most other scripting languages (Perl, Python and shell) allow similar constructs.


There are differences between foreach and foreach-object.

A very good description you can find here: MS-ScriptingGuy

For testing in PS, here you have scripts to show the difference.

ForEach-Object:

# Omit 5.1..10 | ForEach-Object {if ($_ -eq 5) {return}# if ($_ -ge 5) {return} # Omit from 5.Write-Host $_}write-host "after1"
# Cancels whole script at 15, "after2" not printed.11..20 | ForEach-Object {if ($_ -eq 15) {continue}Write-Host $_}write-host "after2"
# Cancels whole script at 25, "after3" not printed.21..30 | ForEach-Object {if ($_ -eq 25) {break}Write-Host $_}write-host "after3"

foreach

# Ends foreach at 5.foreach ($number1 in (1..10)) {if ($number1 -eq 5) {break}Write-Host "$number1"}write-host "after1"
# Omit 15. foreach ($number2 in (11..20)) {if ($number2 -eq 15) {continue}Write-Host "$number2"}write-host "after2"
# Cancels whole script at 25, "after3" not printed.foreach ($number3 in (21..30)) {if ($number3 -eq 25) {return}Write-Host "$number3"}write-host "after3"


To stop the pipeline of which ForEach-Object is part just use the statement continue inside the script block under ForEach-Object. continue behaves differently when you use it in foreach(...) {...} and in ForEach-Object {...} and this is why it's possible. If you want to carry on producing objects in the pipeline discarding some of the original objects, then the best way to do it is to filter out using Where-Object.