Evaluating Exit Codes (aka Error Level – Part 2)

by Apr 25, 2017

When you directly launch a console-based application, PowerShell returns its exit code (aka Error Level) in the automatic variable $LASTEXITCODE. However, how do you get the exit code for a console-based application that you launch via Start-Process?

Here is how:

$hostname = 'powershellmagazine.com'
# run the console-based application synchronously in the PowerShell window, 
# and return the process object (-PassThru)
$process = Start-Process -FilePath ping -ArgumentList "$hostname -n 2 -w 2000" -Wait -NoNewWindow -PassThru

# the Error Level information is then found in ExitCode:
$IsOnline = $process.ExitCode -eq 0
$IsOnline

Twitter This Tip! ReTweet this Tip!