Get Exit Code of Last Command in PowerShell

Get exit code of Last command in PowerShell

We can use different solutions to get the exit code of the last command in PowerShell, but before that, it is essential to know what does exit command means.

The exit command terminates the current PowerShell script or session in PowerShell. When an exit command is executed, the PowerShell session or script will be immediately closed, and any further commands or statements will not be executed.

In the above code, the script checks if an $error_message variable contains any value, then writes a message An error occurred, exiting with code 1 on the console and exit with an exit code 1. Otherwise, display the message This is the end of the script on the console. When the script exits at this point, it will exit with a default exit code of 0. Finally, the script will wait for 3 seconds before exiting.

In the above case, the $error_message variable is not empty after executing the above script. For example, the message An error occurred, exiting with code 1 will be displayed on the console, and after 3 seconds, the script will terminate with exit code 1. To see the exit code, we can run the automatic variable $LASTEXITCODE we will discuss in the next section).

Let’s continue with various ways to get the exit code of the last command in PowerShell.

Using Automatic Variable $LASTEXITCODE

To get the exit code of the last command executed in PowerShell, use the automatic variable $LASTEXITCODE.

In this example, the ping command tests connectivity to a network host. Ping in PowerShell uses ICMP echo request packets to send a series of request packets to the specified host and wait for a response packet. After the ping command completes, it sets the $LASTEXITCODE environment variable to indicate whether the command succeeded or failed.

In the above example, the ping command successfully received an ICMP echo reply packet from the Facebook server, and the $LASTEXITCODE variable was 0.

The $LASTEXITCODE variable returns the exit code as Boolean values.

  • 0: The command or script was completed successfully without errors.
  • 1: The command or script encountered one or more errors during execution.

Let’s see another example to understand how $LASTEXITCODE works.

In the above script file, the ping facebok.com command is used to test connectivity to the facebok.com server. After the ping command, the if statement checks the value of $LASTEXITCODE.

If the exit code equals 0, it will print a message saying that the Ping succeeded. If the exit code is 1, it will print the Ping failed message with the given exit code.

Here, we can see that the ping command failed with an exit code of 1, and the message is printed accordingly.

Using Automatic Variable $?

To check the status of the exit code of the last command executed in PowerShell, use the automatic variable $?.

Here, we have encountered an error because we missed the $ sign at the start of variable x. Now Let’s check the exit status of the last command using $? below:

We can observe False is returned, which means the last command is not executed successfully. Therefore, there are two possible values of the automatic variable $?, which are as follows:

  • TRUE – Successfully executed. It means the last command is executed without error.
  • FALSE – Unsuccessful execution. It means the last command is exited with an error

Now, let’s make a correction in the command and check its exit status:

We can observe now TRUE is returned, showing the status of the last command being executed without any error.

That’s all about how to get exit code of last command in PowerShell.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *