Search notes:

PowerShell: the automatic variable $lastExitCode

$lastExitCode is the return value of the most recently executed executable (on Windows: .exe) or PowerShell script.
The type of $lastExitCode is Int32.
In contrast, $? is a Boolean which indicates the succuess or failure of the last command, regardless if it was an executable or a PowerShell cmdlet or function.

See also

The cmd.exe variable %errorlevel%.
Other automatic variables
My profile.ps has the following piece of code which uses the prompt to write the last exit code in red if it is different from 0:
function prompt {
   if ($lastExitCode) {
      $error = "$([char]0x1b)[91mlastExitCode = $lastExitCode$([char]0x1b)[0m`n"
   }
   else {
      $error = ''
   }
   …
   $prompt = "…"
  "$error$prompt"

Index