Error does not stop script from running

In this simple code the script does not stop upon encountering error at the catch block. I want to export a log after a script has been run.

What am I doing wrong?

    $items =  Import-Csv -Path "C:\WRONG PATH" -ErrorAction Stop
    $output = foreach ($item in $items)
  Write-Warning $Error[0]

Write-Output "Successfully Run"

I’d say “Nothing”. PowerShell does exactly what’s expected. If there is a terminating error in the try block it will trigger the catch block. There you specified to output the error as warning. Then you explicitly write to the default ouput “Successfully Run”.


oh, I was expecting Error Action = Stop to stop the rest of the script from running when there is any kind of error (terminating or non-terminating).

If you would not have any error handling that would be the case. But for that reason we use error handling. To avoid to confront the user with an unhandled error. :wink:

Just remove the try catch block and you will see. :wink:

BTW: only with a terminating error the script would stop. A non-terminating error would not terminate the script execution. :wink:

1 Like