PowerShell Exit function terminates a script. Using the Exit keyword in PowerShell can terminate a console or script in scope. It stops PowerShell script.
Using Exit cmdlet in PowerShell, you can exit the remote PowerShell session.
In this article, we will discuss how to use the exit keyword in PowerShell to exit the script.
We will discuss with examples other cmdlets which are used in the script as an Exit function like Exit, Break, Return and Exit-PSSession.
Let’s practice!
Using PowerShell Exit keyword
Exit keyword in PowerShell can terminate the script, console session.
If you open the PowerShell console and type exit, it will immediately close.
PS C:\> Exit
Using the Exit keyword in a script terminates only the script and not the console session from where we run the script.
Save the script in the file as FuncTest.ps1
Function ExitFunctionTest{ Write-Output "Inside function..." Exit }
The next step is to call the script from the console.
It will display the “Inside Function…” message on the console and stop PowerShell script.
The console will remain open as is.
Cool Tip: PowerShell echo equivalent command in Windows!
Using Break Keyword
Break keyword is mainly used in loops in PowerShell like ( For, while, foreach, do-while, etc..).
It exits from the current loop.
for($count =1; $count -lt 5;$count++) { if($count -eq 3) { break} Write-Output "Current Counter Value:" $count }
In the above example, we have used for loop to demonstrate PowerShell exit functionality using the break keyword.
$count variable value increment by +1 on each iteration.
Inside the for loop, it checks for conditions if the value of $count is equal to 3, it executes the break statement.
This will terminate the for loop and exit for a loop.
It stops PowerShell script and starts executing the next statement after for loop.
The output of the above PowerShell exit script using break is:
Cool Tip: How to pass multiple parameters to function in PowerShell!
Using Return Keyword
Using the Return keyword in the PowerShell script returns to the point where it invokes it.
Return keyword doesn’t terminate the script or session.
Let’s understand the return keyword with an example.
Function CheckStatus ($bitStatus){ If($bitStatus = 2) { return $true } else { return $false } Write-Output "Bit Status check function called" } Write-Output "Call to Bit Status check function" CheckStatus(2) # Invoke the function Write-Output "CheckStatus function called"
In the above example of PowerShell exit script,
function CheckBitStatus returns the true if bitStatus is equal to 2.
You can return any type of value.
Cool Tip: Using Active Directory UserAccountControl flags in PowerShell!
The output of the above function is:
Call to Bit Status check function
True
CheckStatus function called
After evaluating the condition, it exit from the function returns to the point from where it was called and continues with further statement execution.
Cool Tip: How to concatenate string in PowerShell!
Conclusion
PowerShell Exit keyword can terminate the script, loop, console. PowerShell exit in function stops PowerShell script.
Using the break keyword inside the script or loop, it will exit the current scope of that script or loop.
Using the return keyword in the script, it will return to the point from where it was called.
Cool Tip: How to add a newline to string in PowerShell!
Using the return keyword, you can return any type of value.
Each of these keywords has its own use case.
Cool Tip: How to use PowerShell NoExit to keep PS console windows open!
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.