Determining if a Powershell command was successful Determining if a Powershell command was successful powershell powershell

Determining if a Powershell command was successful


Assuming appcmd is a console exe, even if it errors, the next line in the script will execute.

If you want to test if the EXE errored and the EXE uses the standard 0 exit code to indicate success, then just inspect the $? special variable right after calling the EXE. If it is $true, then the EXE returned a 0 exit code.

If the EXE is non-standard in terms of the exit code it returns for success (perhaps it has multiple success codes) then inspect $LastExitCode to get the exact exit code the last EXE returned.