Table of Contents
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.
1 2 3 4 5 6 7 8 9 |
$error_message= "There is an error" if ($error_message) { Write-Host "An error occurred, exiting with code 1" Start-Sleep -Seconds 3 exit 1 } Write-Host "This is the end of the script" |
1 2 3 |
An error occurred, exiting with code 1 |
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
.
1 2 3 4 |
ping facebook.com $LASTEXITCODE |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Pinging facebook.com [157.240.227.35] with 32 bytes of data: Reply from 157.240.227.35: bytes=32 time=64ms TTL=55 Reply from 157.240.227.35: bytes=32 time=64ms TTL=55 Reply from 157.240.227.35: bytes=32 time=65ms TTL=55 Reply from 157.240.227.35: bytes=32 time=91ms TTL=55 Ping statistics for 157.240.227.35: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds: Minimum = 64ms, Maximum = 91ms, Average = 71ms 0 |
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.
1 2 3 4 5 6 7 8 |
ping facebok.com if ($LASTEXITCODE -eq 0) { Write-Host "Ping succeeded." } else { Write-Host "Ping failed." } |
1 2 3 4 5 6 7 8 9 |
Directory: D:\U - A\PowerShell Mode LastWriteTime Length Name ---- ------------- ------ ---- d----- 3/13/2023 9:59 PM Uploaded -a---- 3/17/2023 8:25 PM 5 helo.txt -a---- 3/17/2023 9:15 PM 245 hi.ps1 Command succeeded |
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.
Further reading:
Using Automatic Variable $?
To check the status of the exit code of the last command executed in PowerShell, use the automatic variable $?
.
1 2 3 |
x = 65 |
1 2 3 4 5 6 7 8 9 |
x=65 : The term 'x=65' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + x=65 + ~~~~ + CategoryInfo : ObjectNotFound: (x=65:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException |
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:
1 2 3 |
$? |
1 2 3 |
FALSE |
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:
1 2 3 4 |
$x=6 $? |
1 2 3 |
TRUE |
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.