Detecting error after a powershell statement is executed


I am using powershell to run sqlplus and I would like PowerShell to detect if there are error after the script was run and to perform some action instead of me looking at the result file.

Normally in DOS, there is %errorlevel% when the command encounters error and I wonder if there is similar stuff in PowerShell?

Of course, I can read the log file myself but sometimes, thing got too routine and I may forget.

My Test.sql:

There is clearly a division by zero error. The result.txt captures it but I would like powershell to detect it as well

Does the powershell statement return an errorlevel after the statement is executed like DOS?

I have tried:

But that has caused syntax error?

errorlevel : The term ‘errorlevel’ is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the
name, or if a path was included, verify that the path is correct and
try again.

What is a proper way to check error in powershell?


I used this:


Since you are invoking an executable, you probably want to check for the $LASTEXITCODE variable or the return value of sqlplus. In PowerShell each variable has a $ prefix.


Detecting error after a powershell statement is executed by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply