Check last exit code with PowerShell

In a script containing a lot of commands, it could be very helpful to be able to check if their execution are successful or not.

To check the last exit code returned in a PowerShell script, the variable $lastExitCode can be used.

$lastExitCode contains the exit code of the last Win32 executable execution.

Here is an example: