Evaluating Exit Codes (aka Error Level – Part 1)

by Apr 24, 2017

When running a console-based application, it typically returns a numeric exit code. The meaning of this exit code is up to the console-based application, and you’d have to look it up there. PowerShell does hand you the exit code, though. It surfaces in $LASTEXITCODE.

Here is an example using ping.exe to test for network response:

$hostname = 'powershellmagazine.com'
# run console-based executable directly
# and disregard text results
$null = ping.exe $hostname -n 2 -w 2000
# instead look at the exit code delivered in
# $LASTEXITCODE. Ping.exe returns 0 if a 
# response was received:
$IsOnline = $LASTEXITCODE -eq 0

Twitter This Tip! ReTweet this Tip!