How to Use the PowerShell Exit Command and Friends

choubertsprojects

The Best WordPress plugins!

1. WP Reset

2. WP 301 Redirects

3. WP Force SSL

Sometimes you need to close a PowerShell session and its not always easy. The Exit command is your friend in these situations.

The “how to exit powershell command prompt” is a useful tool for those who are using the PowerShell. The command line allows users to exit from the current shell, or any other shell that they have open.

How to Use the PowerShell Exit Command and Friends

“What is the best approach to end a script or quit a command in PowerShell?” you may question. Will using the PowerShell exit command end the PowerShell session? How can I get custom exit codes back? You’ve come to the right place!

These and other questions will be addressed in this handbook. Learn how to quit a PowerShell script or session in a variety of ways.

Let’s get some PowerShell sessions and scripts terminated!

Prerequisites

Make sure you have Windows PowerShell or PowerShell Core installed on your computer to follow along. PowerShell 7.1.4 will be used in the examples in this tutorial.

With exit, you may shut off the PowerShell console and scripts.

Let’s get started with this lesson by looking at the exit keyword, which is one of PowerShell’s numerous reserved terms. A reserved word is a group of letters that performs a specific operation in PowerShell. The exit keyword causes PowerShell to terminate a PowerShell session.

Let’s get some practice in! Open a PowerShell console window, type exit, and then click Enter. The PowerShell console will be closed immediately.

Instead of exiting the console session, this term may also quit a script. When the exit keyword is used in a script, it simply ends the script, not the full console session from which it was executed.

Save the script as test after copying and pasting the code below into notepad. ps1 to the directory C:Temp As seen in the picture below, execute the PowerShell script file with the command.text.ps1. PowerShell does not close the session; instead, it ends the script and restores control to the current PowerShell session’s prompt.

# test.ps1 is a.ps1 file that contains a Write-Host “Quick! Before the PowerShell session ends, read this!!” 2 Exit Start-Sleep-Seconds

The exit keyword terminated the script, but the console remained open. The exit keyword terminated the script, but the console remained open.

Exit Codes for Receiving and Giving Feedback

When you require a script or function to execute some code and then quit, the exit command comes in handy. However, you have no way of knowing if the script worked or not.

When PowerShell executes the final command in a script, the $LASTEXITCODE variable is set to the exit code of that command.

There are two exit codes by default:

Exit codes in PowerShell are not limited to 0 and 1. Custom exit codes may be returned by PowerShell as long as they are integers.

The exit code’s range of values is platform-dependent. Any 32-bit signed integer is acceptable on Windows. Only positive 8-bit integers are permitted under Unix.

You may provide the exit code as an argument to the exit keyword to quit PowerShell with a custom exit code.

In the current test, copy and paste the code below. ps1 file, then use the command to start the script. Test. ps1. Examine the $LASTEXITCODE variable’s content. As you can see in the screenshot below, instructing PowerShell to depart with a 55 exit code caused it to do so.

Function foo { Write-Output “This foo function will be shown” Exit 55 # < — Using custom exit code } foo

Obtaining a unique exit code Obtaining a unique exit code

Using Break to Break Out of Loops

There will be times where departure behavior is unacceptable, such as when exiting a function. Let’s take a look at how the break keyword might assist.

PowerShell uses the break keyword to break a loop. The code continues to execute at the following iteration after a break.

Breaks are useful in PowerShell loops like the foreach, while, do-while, and switch statements because they allow you to determine where your code stops before it finishes.

Let’s work on getting out of loops. A example of code containing a for loop is shown below. If not for the break inside the for loop, it would repeat 10 times. When the iterator equals eight, the break keyword will be used. The loop is broken whenever PowerShell encounters the break keyword.

for($i=1; I -le 10; $i++) if($i -eq 8) if($i -eq 8) if($i -eq 8) if($i -eq 8) if($i -eq 8) if($i -eq 8) if(

When I = 8, PowerShell ends the loop, as seen below. The code is never set to nine. You’ve managed to break out of a for loop.

Getting out of a for loop Getting out of a for loop

Back to the Basics with PowerShell For Loop

Using Return to Redirect Code Execution

The return keyword differs from the others in that it does not terminate or break instructions. This keyword causes code execution to be redirected. It allows you to return values while returning execution to its call. Mind you, not simply integers like exit. You are free to return any kind! Because of these characteristics, the return keyword is more adaptable than the preceding keywords.

Associated: Powershell Functions: A Beginner’s Guide

Let’s look at an example: creating a function that pairs words. In your PowerShell session, paste the following code:

function WordPairing ($a,$b) { return “$a” + “$b” # <– Combining two words into one word } $output = WordPairing taco cat Write-Output “$output is a strange pairing of words.”

Take note of how the value you return from the function appears in the output.

The return keyword is used to redirect code execution. The return keyword is used to redirect code execution.

The $output created by the WordPairing function is redirected back to the $output function.

Conclusion

You’ve learnt how to terminate PowerShell sessions and scripts in this tutorial. Each approach is based on a distinct keyword. Each term has a specific use. Each of these keywords, when utilized correctly, allows you a lot of control over what your script performs.

How do you intend to use this newfound information to better your scripts now that you know several methods to quit your PowerShell sessions?

The “powershell exit function but not script” is a problem that can occur when using PowerShell. The issue occurs when the user tries to use the “exit function” with a script. This causes an error in the script, and doesn’t allow it to be executed properly.

Frequently Asked Questions

How do I get out of exit code in PowerShell?

A: The exit code for PowerShell is 0.

How do I set exit code in PowerShell?

A: The best way to set an exit code from a PowerShell script is by using the Exit-Script cmdlet. This will allow your program to leave with one of three possible codes, 0 meaning success, 1 meaning failure and 2 meaning that there was an error while running.

Related Tags

  • powershell exit script without closing window
  • powershell exit codes list
  • powershell exit code of last command
  • powershell stop command ctrl+c
  • powershell exit 1

Table of Content