82

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Joey
  • 344,408
  • 85
  • 689
  • 683

2 Answers2

123

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
Sam Rueby
  • 5,914
  • 6
  • 36
  • 52
zdan
  • 28,667
  • 7
  • 60
  • 71
  • 5
    According to `Get-Help Exit-PSSession` _You can also use the Exit keyword to end an interactive session. The effect is the same as using Exit-PSSession._ So it also says `Exit` is a keyword. However, it appears that the quote cannot be taken to imply that `Exit` and `Exit-PSSession` are equivalent in all cases. The exit keyword can be used with an argument which can convert to `[int]`, and in that case that number will be the exit code. For example `Exit 42`. – Jeppe Stig Nielsen Oct 14 '15 at 09:20
  • 2
    I have to say, this answer is completely and demonstrably false. I have put a function in my $profile and in that function I put an "exit" on a certain condition and it does NOT exit the function, it exits the entire console. I've wasted quite a few hours searching what can leave a function wihout killing the console session and I'm stumped. The above is definitely wrong. – YorSubs Dec 25 '19 at 20:06
  • So, maybe this behaviour is because $profile is dotsourced into the current session? In that case, then the `exit` inside a function there completely fails to do what is described by Bruce Payette in the above. If that is the case, then that part of the answer is just wrong, because a dotsourced script is *still* a script that is called by the console session, so it's still a script(!), but how then can we exit a function without quitting the console session if the script has been dotsourced? – YorSubs Dec 25 '19 at 20:48
  • @YorSubs if you read the linked reference, it notes that `exit` "Causes PowerShell to exit a script or a PowerShell instance." Exiting the instance is the expected behavior. If you want to exit a function, you can simply use `return`. From the same reference: "Causes PowerShell to leave the current scope, such as a script or **function**, and writes the optional expression to the output." That `return` for return values (including void) did not become idiomatic in PowerShell functions is bizarre coming from other programming languages, but that's besides the point. – PartyLich Feb 18 '20 at 16:10
-1

If you want a ^d alias to exit, try doing the exit inside of a script block invocation.

Invoke-Expression "Function $([Char]4) { Invoke-Command -ScriptBlock { Exit } }"
durette
  • 353
  • 1
  • 12