PowerShell 中命令前的與號 (&) 是什麼?

PowerShell 中命令前的與號 (&) 是什麼?

我透過輸入命令的路徑來嘗試命令,當我點擊 時TAB,發生了這種情況:

C:\> C:\Program Files\KeePassXC\keepass<TAB>
# became this:
C:\> & 'C:\Program Files\KeePassXC\keepassxc-cli.exe'

它只是評估一個字串嗎?至少,我認為是這樣,因為路徑需要用引號括起來,因為其中有空格,並且不會&出現以下錯誤:

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

您能連結到文件嗎?謝謝你!


討論的主題&(1,2)完全是關於其他事情(或者我錯過了找到聯繫)。

答案1

這是一個呼叫或呼叫運算符呼叫接線生 &:

您可以使用呼叫運算子透過檔案名稱執行腳本。下面的範例顯示了包含空格的腳本檔名。當您嘗試執行該腳本時,PowerShell 會顯示包含檔案名稱的引號的字串的內容。呼叫運算符可讓您執行包含檔案名稱的字串的內容。

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

PS C:\Scripts> & ".\script name with spaces.ps1"
Hello World!

相關內容