Updated March 4, 2023
Definition of PowerShell Exit
PowerShell exit functions or cmdlets are the scripts or the function terminators. Using Exit functions in PowerShell doesn’t always terminate the console, it depends on the command and the scope in the script used as the same command can terminate the console and the same command can terminate the function or the script is used in the different scope and Exit cmdlet is also useful for exiting the remote PowerShell session.
Syntax
Few cmdlets are used as an Exit function for the PowerShell and they are as below.
- Exit
- Break
- Return <Expression>
- Exit-PSSession
How does Exit Function work in PowerShell?
Working of exit functions in PowerShell are given below:
1. Exit Keyword
PowerShell Exit Keyword should be used carefully because it can terminate function, console, or even the editors. If the Exit keyword is used in the functions and the main script, it closes everything and you may not get a chance to see the output in the console if not stored before the Exit Keyword is used.
Function ExitTest{
Write-Output "This is written on console but it terminates abruptly"
}
ExitTest #Calling function
Exit
You can’t see the above output because as soon as you run the command it terminates the script. You can use the Sleep command with more seconds (10 or more) or pause to see the output for few seconds.
When the Exit keyword is used inside a function and saved in the script, and once the script is executed from the console, it terminates the function and the script but won’t close the console as shown in the below example.
Function ExitTest{
Write-Output "This will be displayed"
Exit
}
ExitTest #Calling function
Write-Output “This Won’t be displayed but console will not closed”
Output:
2. Break Keyword
The break keyword is used in the loops (For, foreach, while, do-while, etc..) to exit from the current control loop. This Break statement is also used with the Switch statement as well.
When the break statement is used for the single loop, it exits the entire loop when the Break statement is executed.
for($i=1; $i -lt 10; $i++){
if($i -eq 5) { break }
$i
}
In the above example, when the value of the variable $i becomes 5, it executes the Break statement, and the loop main For loop is terminated. The output is as shown below.
Output:
Let’s take the nested loop example. In the below example, when the value of the $i variable becomes 2, the break statement is executed.
for($i=1;$i -lt 5;$i++){
for($j=1; $j -lt 3; $j++){
if($i -eq 2){break}
Write-Output "`ni = $i, j=$j"
}
}
Output:
In the above example, when the inner loop is executed ($j loop) and when it has a break statement, it breaks only that current control loop as shown in the output, when the $i variable value becomes 2, it breaks the $j loop.
If we put the break statement outside, it breaks both the loops as shown below.
for($i=1; $i -lt 5 ;$i++) {
if($i -eq 2) { break }
for($j=1; $j -lt 3; $j++){
Write-Output "`ni = $i, j=$j"
}
}
Output:
3. Return Keyword
The return statement doesn’t terminate the loop or the script but it redirects and returns to the point where it was called. When the return statement is called from the console it doesn’t return to another point but simply prints the value.
return "Hello world"
Output:
And when it is called from the Script, it saves the state of the original point from where it was called and returns to that point again.
function CheckService($ser) {
if( Get-Service $ser -EA Ignore){ return $true}
else {return $false }
}
If( CheckService TestService ) {
Write-Output "Service Exist"
}
else { Write-Output "Service doesn't exist" }
Output:
In the above example, the function name called CheckService returns true if the service is available else it returns false.
Examples of PowerShell Exit
Following are the examples are given below:
Example #1
Using Exit Console with Sleep Command
In this example, we will use the Exit command with the Sleep function so that the script terminates after few specified seconds.
Code:
$i = 0
While($true){
$i
if($i -eq 10){
Write-Output "Console is terminating in 3 seconds"
sleep 3
Exit
}
$i++
}
Here, in the infinite loop, if the value of the $i variable reaches 10, it automatically closes the console after 3 seconds.
Example #2
Using Break statement to exit from the While Loop.
This example checks if the server LabMachine2k16 is online after reboot or not and it continuously tests connection until the server comes up and once the server is up it uses the break command to exit from the loop.
Code:
$Computer = "LabMachine2k16"
while($true){
if( Test-Connection -ComputerName $Computer -Count 2 -Quiet -EA Ignore ){
Write-Output "Server is online"
Break
}
else{
Write-Output "Server is not online yet"
}
}
Output:
Example #3
Using Break Statement to Exit from The Switch Block
We can use the Break command with the Switch block as well. For example,
Code:
$color = Read-Host "Enter Color name "
switch($color){
"Red"{
Write-Output "Red color selected"
Break
}
"Yellow"{
Write-Output "Yellow color selected"
Break
}
Default{
Write-Output "No matching color selected"
Break
}
}
Output:
Once the color name is entered, the switch block is executed and terminated with the Break statement.
Example #4
Using Return statement to return value to the call.
In this example, two values are multiplied in the function, and the output is returned to the main call.
Code:
Function Multiplication($a,$b){
return $a*$b
}
$out = Multiplication 5 10
Write-Output "Multiplication: $out"
Output:
Example #5
Using Exit-Ps Session Command to Exit the Remote Session
Exit-PSSession is used to terminate the remote connected session. However, this can’t be used inside the PowerShell script, it is an interactive method. Below commands are executed one by one on the PS console.
Code:
Enter-PSSession LabMachine2k16 #Enters into PS Remote Session
Write-Output "My HostName is $($env:COMPUTERNAME)"
Exit-PSSession
Output:
Conclusion
There are various Exit commands available to exit the console, terminate the loop, or return to the original point of the console and which statement to use is depends on the way you want the script to generate output or script to be terminated.
Recommended Articles
This is a guide to PowerShell Exit. Here we also discuss the definition and How do Exit Functions Work in PowerShell? along with different examples and its code implementation. You may also have a look at the following articles to learn more –