PowerShell 中命令前的与号 (&) 是什么?

tor*_*tte 5 windows powershell command-line

我通过输入命令的路径来尝试命令,当我点击 时TAB,发生了这种情况:

C:\> C:\Program Files\KeePassXC\keepass<TAB>
# became this:
C:\> & 'C:\Program Files\KeePassXC\keepassxc-cli.exe'
Run Code Online (Sandbox Code Playgroud)

它只是评估一个字符串吗?至少,我认为是这样,因为路径需要用引号括起来,因为其中有空格,并且不会&出现以下错误:

C:\> 'C:\Program Files\KeePassXC\keepassxc-cli.exe' --help

At line:1 char:50
+ 'C:\Program Files\KeePassXC\keepassxc-cli.exe' --help
+                                                  ~~~~
Unexpected token 'help' in expression or statement.
At line:1 char:1
+ 'C:\Program Files\KeePassXC\keepassxc-cli.exe' --help
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The '--' operator works only on variables or on properties.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken
Run Code Online (Sandbox Code Playgroud)

您能链接到文档吗?谢谢你!


&讨论( 1 , 2 )的线程完全是关于其他事情的(或者我错过了找到连接)。

小智 7

这是一个调用或调用运算符Call operator &

您可以使用调用运算符通过文件名执行脚本。下面的示例显示了包含空格的脚本文件名。当您尝试执行该脚本时,PowerShell 会显示包含文件名的带引号的字符串的内容。调用运算符允许您执行包含文件名的字符串的内容。

PS C:\Scripts> ".\script name with spaces.ps1"
.\script name with spaces.ps1

PS C:\Scripts> & ".\script name with spaces.ps1"
Hello World!
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

4820 次

最近记录:

3 年 前