When piping the output of a command to a ForEach-Object loop in PowerShell, using a continue statement causes it to error out.
Works as expected:
ipconfig | ForEach-Object {
$_
}
Errors:
ipconfig | ForEach-Object {
$_
continue
}
The error is
Program 'ipconfig.exe' failed to run: System error.At C:\Users\test\ps.ps1:1 char:1
+ nslookup google.com | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~.
At C:\Users\test\ps.ps1:1 char:1
+ nslookup google.com | ForEach-Object {
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException
+ FullyQualifiedErrorId : NativeCommandFailed
This seems to happen with any external command, such as nslookup and net
I thought maybe this had to do with some type conversion, but the object inside the loop is always a string. Saving the command output to a variable, and then doing a $var | ForEach-Object works.
continuestatements in PowerShell can only be used within loop constructs. WhileForEach-Objectseems like a loop, it's not.In PowerShell, you can technically use
continueoutside of a loop, but it causes PowerShell to not find a loop, and just terminate the current runspace.1This means PowerShell can actually terminate the caller1, and looking at the callstack, that caller is the "parent" line and so it is terminated.
Callstack right before the
continueexecutes:Instead, use a
returnstatement.Reference
1 - https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_continue?view=powershell-5.1