about_PowerShell_exe

簡短描述

說明如何使用 powershell.exe 命令行介面。 顯示命令行參數並描述語法。

詳細描述

如需 PowerShell 7 命令行選項的相關信息,請參閱 about_Pwsh

語法

PowerShell[.exe]
    [-PSConsoleFile <file> | -Version <version>]
    [-NoLogo]
    [-NoExit]
    [-Sta]
    [-Mta]
    [-NoProfile]
    [-NonInteractive]
    [-InputFormat {Text | XML}]
    [-OutputFormat {Text | XML}]
    [-WindowStyle <style>]
    [-EncodedArguments <Base64EncodedArguments>]
    [-EncodedCommand <Base64EncodedCommand>]
    [-ConfigurationName <string>]
    [-File - | <filePath> <args>]
    [-ExecutionPolicy <ExecutionPolicy>]
    [-Command - | { <script-block> [-args <arg-array>] }
                | { <string> [<CommandParameters>] } ]

PowerShell[.exe] -Help | -? | /?

參數

-Command

執行指定的命令(以及任何參數),就像是在 PowerShell 命令提示字元中輸入它們,然後結束,除非 NoExit 指定參數。

Command 的值可以是 -、腳本區塊或字串。 如果 Command 的值-,則會從標準輸入讀取命令文字。

當 Command 參數可以將傳遞至 Command 的值辨識為 ScriptBlock 類型時,命令參數只會接受腳本區塊來執行。 只有在從另一個PowerShell主機執行時,才可能這樣做powershell.exeScriptBlock 類型可以包含在現有的變數中、從運算式傳回,或由 PowerShell 主機剖析為以大括弧括住的常值腳本區塊,{}再傳遞至 powershell.exe

powershell -Command {Get-WinEvent -LogName security}

在 中 cmd.exe,腳本區塊(或 ScriptBlock 類型)沒有這類專案,因此傳遞至 Command 的值一 會是字串。 您可以在字串內撰寫腳本區塊,但與其執行腳本區塊的行為完全一樣,就像您在一般 PowerShell 提示字元中輸入它一樣,而是將腳本區塊的內容列印回您。

傳遞至 Command 的字串仍會以 PowerShell 程式代碼的形式執行,因此從執行 時 cmd.exe,通常不需要腳本區塊大括號。 若要執行字串內定義的內嵌文稿區塊, 可以使用呼叫運算子&

powershell.exe -Command "& {Get-WinEvent -LogName security}"

如果 Command 的值是字串,則 Command 必須是 pwsh 的最後一個參數,因為後續的所有自變數都會解譯為要執行的命令的一部分。

從現有的PowerShell工作話內呼叫時,結果會以還原串行化 XML 物件的形式傳回父殼層,而不是實時物件。 對於其他殼層,結果會以字串的形式傳回。

如果 Command 的值-,則會從標準輸入讀取命令文字。 使用命令參數搭配標準輸入時,您必須重新導向標準輸入。 例如:

@'
"in"

"hi" |
  % { "$_ there" }

"out"
'@ | powershell -NoProfile -Command -

這個範例會產生下列輸出:

in
hi there
out

進程結束代碼取決於文本區塊內最後一個 (executed) 命令的狀態。 結束代碼是 0$?$true1 當 為 $false$?。 如果最後一個命令是外部程式或明確設定 或 1以外的0結束代碼的PowerShell腳本,則會將1結束代碼轉換成進程結束代碼。 若要保留特定的結束代碼,請將 新增 exit $LASTEXITCODE 至您的命令字串或腳本區塊。

如需詳細資訊,請參閱 $LASTEXITCODE about_Automatic_Variables

同樣地,當腳本終止 (runspace-terminating) 錯誤,例如 throw-ErrorAction Stop發生,或執行以 Ctrl+C 中斷時,就會傳回值 1。

-ConfigurationName <字串>

指定執行 PowerShell 的組態端點。 這可以是本機計算機上註冊的任何端點,包括預設PowerShell遠端端點或具有特定使用者角色功能的自定義端點。

-EncodedArguments <Base64EncodedArguments>

接受Base64編碼的字串版本命令自變數。 使用此參數來提交需要複雜、巢狀引用的自變數。 Base64 表示法必須是 UTF-16LE 編碼字串。

-EncodedCommand <Base64EncodedCommand>

接受命令的base-64編碼字串版本。 使用此參數將命令提交至需要複雜引號或大括弧的PowerShell。 字串必須使用UTF-16LE字元編碼來格式化。

-ExecutionPolicy <ExecutionPolicy>

設定目前會話的預設執行原則,並將它儲存在環境變數中 $env:PSExecutionPolicyPreference 。 此參數不會變更登錄中設定的PowerShell執行原則。 如需 PowerShell 執行原則的相關信息,包括有效值的清單,請參閱 about_Execution_Policies

-File - | <filePath><args>

如果 File 的值-,則會從標準輸入讀取命令文字。 在沒有重新導向標準輸入的情況下執行 powershell -File - 會啟動一般會話。 這與完全未指定 File 參數相同。

如果 File 的值 是 filepath ,腳本會在新工作階段的本機範圍 (“dot-sourced”) 中執行,讓腳本所建立的函式和變數可在該新會話中使用。 輸入腳本 filepath 和任何參數。 檔案 必須是命令中的最後一個參數。 在 File 參數之後輸入的所有值都會解譯為腳本 filepath 和傳遞至該腳本的參數。

傳遞至腳本的參數會在由目前殼層解譯之後,以常值字串的形式傳遞。 例如,如果您處於 cmd.exe ,而且想要傳遞環境變數值,您可以使用 cmd.exe 語法: powershell.exe -File .\test.ps1 -TestParam %windir%

相反地,在 cmd.exe 中執行powershell.exe -File .\test.ps1 -TestParam $env:windir會導致腳本接收常值字串$env:windir,因為它對目前cmd.exe殼層沒有特殊意義。 $env:windir環境變數參考的樣式可以在 Command 參數內使用,因為它會解譯為 PowerShell 程式代碼。

同樣地,如果您想要從 Batch 腳本執行相同的命令,您可以使用 %~dp0 而非 .\$PSScriptRoot 來表示目前的執行目錄: powershell.exe -File %~dp0test.ps1 -TestParam %windir%。 如果您改用 .\test.ps1,PowerShell 會擲回錯誤,因為它找不到常值路徑 .\test.ps1

當 File 的值是 filepath 時,File必須是命令中的最後一個參數,因為輸入 File 參數名稱之後的任何字元都會解譯為腳本 filepath,後面接著腳本參數。

您可以在 File 參數的值中包含文稿參數和值。 例如:-File .\Get-Script.ps1 -Domain Central

一般而言,腳本的 switch 參數會包含或省略。 例如,下列命令會使用腳本檔案的 Get-Script.ps1 All 參數:-File .\Get-Script.ps1 -All

在罕見的情況下,您可能需要提供 參數的布爾 值。 以這種方式執行腳本時,無法傳遞 switch 參數的明確布爾值。 PowerShell 6 (pwsh.exe) 已移除此限制。

注意

File 參數無法使用預期自變數值陣列的參數來支援腳本。 不幸的是,這是原生命令取得自變數值的方式限制。 當您呼叫原生可執行檔(例如 powershellpwsh)時,它不知道該怎麼處理陣列,因此它會以字串的形式傳遞。

當文稿檔案以 exit 命令終止時,進程結束代碼會設定為搭配 exit 命令使用的數值自變數。 一般終止時,結束代碼一律 0為 。

如需詳細資訊,請參閱 $LASTEXITCODE about_Automatic_Variables

-InputFormat {Text |XML}

描述傳送至 PowerShell 的數據格式。 有效值為 Text (文字字串) 或 XML (串行化 CLIXML 格式)。

-Mta

使用多線程 Apartment 啟動 PowerShell。 此參數是在 PowerShell 3.0 中引進的。 在 PowerShell 2.0 中,多線程 Apartment (MTA) 是預設值。 在 PowerShell 3.0 中,單個線程 Apartment (STA) 是預設值。

-NoExit

在執行啟動命令之後,不會結束。

-NonInteractive

此參數是用來建立不應該需要使用者輸入的會話。 這適用於在排程工作或 CI/CD 管線中執行的腳本。 任何嘗試使用互動式功能,例如 Read-Host 或確認提示,都會導致語句終止錯誤,而不是停止回應。

在啟動時隱藏著作權橫幅。

-NoProfile

不會載入 PowerShell 設定檔。

-OutputFormat {Text |XML}

決定如何格式化 PowerShell 的輸出。 有效值為 Text (文字字串) 或 XML (串行化 CLIXML 格式)。

-PSConsoleFile <FilePath>

載入指定的 PowerShell 控制台檔案。 輸入主控台檔案的路徑和名稱。 若要建立主控台檔案,請使用 PowerShell 中的 Export-Console Cmdlet。

-Sta

使用單個線程 Apartment 啟動 PowerShell。 在 Windows PowerShell 2.0 中,多線程 Apartment (MTA) 是預設值。 在 Windows PowerShell 3.0 中,單個線程 Apartment (STA) 是預設值。

-Version <PowerShell 版本>

啟動指定的PowerShell版本。 有效值為 2.0 和 3.0。 您指定的版本必須安裝在系統上。 如果計算機上安裝 Windows PowerShell 3.0,“3.0” 是預設版本。 否則,“2.0” 是預設版本。 更多資訊,請參閱安裝 PowerShell

-WindowStyle <視窗樣式>

設定工作階段的視窗樣式。 有效值為 NormalMinimizedMaximizedHidden

-説明-?/?

顯示的說明 PowerShell.exe。 如果您要在PowerShell工作階段中輸入 PowerShell.exe 命令,請在命令參數前面加上連字元 (-),而不是正斜線 (/)。 您可以在 中使用 cmd.exe連字元或正斜線。

REMARKS

疑難解答注意事項:在 PowerShell 2.0 中,從 PowerShell 控制台啟動某些程序失敗,並出現 lastExitCode 為 0xc0000142。

例子

# Create a new PowerShell session and load a saved console file
PowerShell -PSConsoleFile sqlsnapin.psc1

# Create a new PowerShell V2 session with text input, XML output, and no logo
PowerShell -Version 2.0 -NoLogo -InputFormat text -OutputFormat XML

# Execute a PowerShell Command in a session
PowerShell -Command "Get-EventLog -LogName security"

# Run a script block in a session
PowerShell -Command {Get-EventLog -LogName security}

# An alternate way to run a command in a new session
PowerShell -Command "& {Get-EventLog -LogName security}"

# To use the -EncodedCommand parameter:
$command = "dir 'c:\program files' "
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
powershell.exe -encodedCommand $encodedCommand