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.
continueas the very last statement in the block?