EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Exit
 

PowerShell Exit

Updated March 4, 2023

PowerShell Exit

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

PowerShell Exit-1.1

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:

PowerShell Exit-1.2

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:

PowerShell Exit-1.3

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:

PowerShell Exit-1.4

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:

PowerShell Exit-1.3..

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:

PowerShell Exit-1.5

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:

Output-1.6

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:

Output-1.7

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:

Output-1.8

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:

Output-1.9

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 –

  1. Start PowerShell from cmd
  2. PowerShell Match
  3. PowerShell JSON Format
  4. Powershell Copy File

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW