I am using Invoke-Expression on each line of a file. If one of the lines causes an error, I would like to stop executing. I have tried using -ErrorAction Stop but it has no effect.
What is supposed to be the effect of the following?
gc -Path "fileWithErrorOnSomeLine.txt" | % {Invoke-Expression "$_" -ErrorAction Stop }
How can one exit the loop immediately if the expression invocation creates an error?
Note: The following change does not stop iterating through the file contents
gc -Path "fileWithErrorOnSomeLine.txt" | % {Invoke-Expression "$_" -ErrorAction Stop } -ErrorAction Stop