tags:

views:

2269

answers:

4

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. 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.

A: 

Not the answer but you can do this:

PS> function exit {exit}
PS> set-alias quit exit
PS> quit

or even shorter:
function quit {exit}
Preet Sangha
I know and I do, but still doesn't actually answer the question.
Joey
A: 

I was going to say that it's a flow control statement, like if or return, but I searched the grammar and couldn't find it.

Scott Weinstein
Funnily that grammar has flaws. For example it mandates `;` in the `for` statement, while you can in fact leave them out at the end. `for(){}` is a valid endless loop and doesn't require `for(;;)`. I wouldn't be too surprised if there are other discrepancies in there :-)
Joey
+9  A: 

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
zdan
Nice find. Thanks.
Joey
A: 

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

Guess, this fact is known

PS C:\> man exit

NAME
    Exit-PSSession

# and so on
Oleg Svechkarenko
No, definitely not. `Get-Help` will present the matching topic, if there is only one that starts like that. In this case it is `Exit-PSSession`. Go ahead and try, but `Exit-PSSession` is not the same as `exit`. You can try the same with `man exi` which will yield `Exit-PSSession` as well, but `exi` is not a command and will result in an error.
Joey
Sure. My mistake
Oleg Svechkarenko