您可以输入exit
退出PowerShell。到现在为止还挺好。但究竟是什么呢?
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
所以它既不是cmdlet,也不是函数,脚本或程序。它留下了究竟是什么的问题。
遗憾的是,这也意味着无法为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
还有更多这样的命令没有命令吗?
ETA:仅供参考:我知道我可以简单地将其包装成一个函数。我的个人资料有
行# exit with Ctrl+D
iex "function $([char]4) { exit }"
在他们中间。我的问题只是要知道这个命令到底是什么。
答案 0 :(得分:79)
这是一个保留关键字(如return,filter,function,break)。
另外,根据Bruce Payette Powershell in Action的第7.6.4节:
但是当您希望脚本从该脚本中定义的函数中退出时会发生什么? ... 为了简化这一过程,Powershell拥有退出关键字。
当然,正如其他人所指出的那样,通过在函数中包装exit来做你想做的事并不难:
PS C:\> function ex{exit}
PS C:\> new-alias ^D ex