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

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/Windows/...', 1759382465, '216.73.216.42', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/Windows/PowerShell/language/variable/automatic/lastExitCode(58): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78