Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is "exit" in PowerShell?

Tags:

powershell

You can exit PowerShell by typing exit. So far so good. But what exactly is this?

PS Home:\> gcm exit Get-Command : The term 'exit' is not recognized as the name of a cmdlet, function, script file, or operable program. Ch eck the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:4 + gcm <<<<  exit     + CategoryInfo          : ObjectNotFound: (exit:String) [Get-Command], CommandNotFoundException     + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand 

So it's neither a cmdlet, function, script or program. It leaves the question what exactly it is.

This unfortunately also means that one can't create aliases to exit:

PS Home:\> New-Alias ^D exit PS Home:\> ^D Cannot resolve alias '♦' because it refers to term 'exit', which is not recognized as a cmdlet, function, operable prog ram, or script file. Verify the term and try again. At line:1 char:2 + ♦ <<<<     + CategoryInfo          : ObjectNotFound: (♦:String) [], CommandNotFoundException     + FullyQualifiedErrorId : AliasNotResolvedException 

Are there any more such commands which are no commands?

ETA: Just for reference: I know I can simply wrap it into a function. My profile has the lines

# exit with Ctrl+D iex "function $([char]4) { exit }" 

in them. My question was just to know what exactly this command is.

like image 409
Joey Avatar asked Aug 13 '09 22:08

Joey


People also ask

How exit PowerShell command line?

To end a Windows PowerShell session in a Command Prompt window, type exit . The typical command prompt returns.

How do I stop a PowerShell function?

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.

How do I run a PowerShell script without exit?

PowerShell NoExit switch prevents the PowerShell console window from closing after running the script. When you double click on the PowerShell script file, or run with the PowerShell option, the script will execute quickly and then disappear.


1 Answers

It's a reserved keyword (like return, filter, function, break).

Reference

Also, as per Section 7.6.4 of Bruce Payette's Powershell in Action:

But what happens when you want a script to exit from within a function defined in that script? ... To make this easier, Powershell has the exit keyword.

Of course, as other have pointed out, it's not hard to do what you want by wrapping exit in a function:

PS C:\> function ex{exit} PS C:\> new-alias ^D ex 
like image 133
zdan Avatar answered Oct 03 '22 22:10

zdan