title: PowerShell and process exit codes
body: <p>This self-answered question tries to address two distinct aspects of dealing with <a href="https://en.wikipedia.org/wiki/Exit_status" rel="noreferrer">process exit codes</a> in PowerShell:</p>
  
  <ul>
  <li><p>In PowerShell code, how can you query the exit code set by an external process (a call to an external program), and how do such exit codes integrate with PowerShell's error handling?</p></li>
  <li><p>When someone else calls PowerShell via its CLI, <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_pwsh" rel="noreferrer"><code>pwsh</code></a> (PowerShell Core) / <a href="https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe" rel="noreferrer"><code>powershell.exe</code></a> (Windows PowerShell), what determines the PowerShell process' exit code  that communicates success vs. failure to the calling process (which could be a build / CI / automation-server task, a scheduled task, or a different shell, for instance).</p></li>
  </ul>