Powershell scripts return the state of the execution, often known as the “return code” or “PowerShell exit code.” A script that runs successfully produces a 0, but one that does not typically returns a non-zero result that can be read as an error code. The exit status is determined by the last command that was carried out in the function or script.
How to Use the PowerShell Exit Command
You’ll find the answers to these and other questions in this manual. Find out how to end a PowerShell script or session in every possible way. Also, you can use Active Directory UserAccountControl flags in PowerShell.
The exit command produces an exit code if it is successfully executed:
- 0 indicates a regular termination.
- whereas 1 denotes failure.
The PowerShell built-in variable $LASTEXITCODE holds the outcome of the error code.
How To Use PowerShell Exit Code to Terminate the Console and Scripts?
One of the numerous reserved terms in PowerShell is the exit keyword or cmdlet. A group of characters in PowerShell known as a reserved word fulfills a specific function. PowerShell exits a PowerShell session when the exit function is used in a script.
- Launch a PowerShell console session, enter the word “exit,” and then press Enter. The PowerShell console will shut down instantly.
- The console session can be closed using this keyword as well as a script.
- A script that doesn’t contain the exit keyword will just end the script itself, not the complete console session it was running in.
- The script should be saved as a Sample when you copy and paste the code below.
#Sample.ps1
Write-Host "THIS POWERSHELL SESSION IS ABOUT TO CLOSE"
Start-Sleep -Seconds 5
Exit
- Run the PowerShell script to exit the console. Take note that PowerShell ends the script instead of terminating the session and restores control to the current PowerShell session’s prompt.
How to Use The Break Keyword In PowerShell Script?
By using the break keyword, PowerShell can end a loop. The code then resumes execution at the following iteration.
PowerShell loops like the foreach, while, do-while, and switch statements all benefit from breaks because they let you decide where your program will end before it runs to completion.
To show PowerShell exit capabilities using the break keyword, I created a loop.
If the value of $count is greater than 6, the break statement is executed inside the for loop, which checks for conditions. With this, the for loop will come to an end.
for($count =1; $count -lt 10;$count++) {
if($count -eq 6)
{ break}
Write-Output "Current Value:" $count
}
It halts the PowerShell script and begins the following command after the for a loop.
The following is the result of the PowerShell exit script’s break command:
Redirecting Code Execution with PowerShell Return Keyword
Given that it doesn’t break or exit commands, the return keyword differs differently from the Powershell exit command. Code execution is redirected by this keyword.
The return command won’t close it if it’s executed from the console; it will simply return the value. The remaining commands won’t be executed if you run it from a script; instead, it will return the value and end the script.
It allows you to return values while passing execution back to its call. Mind you, not simply integers like exit. The return keyword is more adaptable than the preceding keywords because of these properties.
Take note of the output’s display of the value your function is returning. The WordPairing function’s generated $output is redirected back to the $output caller.
function WordPairing ($a,$b) {
return "$a" + "$b"
}
$output = WordPairing kat kit
Write-Output "$output is a unknown pair of words."