about_Prompts

適用於: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

主題

about_Prompts

簡短描述

說明 Prompt 函式並示範如何建立自訂的 Prompt 函式。

詳細描述

Windows PowerShell 命令提示字元表示 Windows PowerShell 已準備好執行命令:

        PS C:\>

Windows PowerShell 提示由內建的 Prompt 函式來決定。您可以自訂提示,只需要建立您自己的 Prompt 函式並將其儲存在您的 Windows PowerShell 設定檔中。

關於 PROMPT 函式

Prompt 函式決定 Windows PowerShell 提示的外觀。Windows PowerShell 隨附內建的 Prompt 函式,但是您可以藉由定義自己的 Prompt 函式來加以覆寫。

Prompt 函式的語法如下:

        function Prompt { <function-body> }

Prompt 函式必須傳回物件。最佳作法是傳回格式化為字串的字串或物件。建議長度上限為 80 個字元。

例如,下列提示函式會傳回 "Hello,World" 的字串,後面接著插入號 (>)。

        PS C:\> function prompt {"Hello, World > "}
        Hello, World > 

取得 PROMPT 函式

若要取得 Prompt 函式,請在 Function 磁碟機中使用 Get-Command Cmdlet 或使用 Get-Item Cmdlet。

Function 是命令。因此您可以使用 Get-Command Cmdlet 來取得函式,包含 Prompt 函數。

例如:

        PS C:\>Get-Command Prompt

        CommandType     Name                                               ModuleName
        -----------     ----                                               ----------
        Function        prompt
        

若要取得設定提示值的指令碼,請使用點方法取得 Prompt 函式的 ScriptBlock 屬性。

例如:

        PS C:\>(Get-Command Prompt).ScriptBlock

        "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
        # .Link
        # https://go.microsoft.com/fwlink/?LinkID=225750
        # .ExternalHelp System.Management.Automation.dll-help.xml

如同所有功能,Prompt 函式會儲存在 Function: 磁碟機中。若要顯示建立目前 Prompt 函式的指令碼,請輸入:

        (Get-Item function:prompt).ScriptBlock

預設的提示

只有當 Prompt 函式產生錯誤或不傳回物件時,才會出現預設的提示。

預設的 Windows PowerShell 提示為:

        PS>

例如,下列命令會將 Prompt 函式設定為 $null,也就是無效。如此一來,預設的提示會隨即出現。

        PS C:\> function prompt {$null}
        PS>

因為 Windows PowerShell 隨附內建提示,所以您通常看不到預設的提示。

內建的提示

Windows PowerShell 包含內建提示函式。

在 Windows PowerShell 3.0 中,內建的提示函式是:

        function prompt
        {
            "PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
        }

這個簡化的提示以 "PS" 開頭,後面加上目前的位置,並在每個巢狀提示層級加上一個 ">"。

在 Windows PowerShell 2.0 中,內建的提示函式是:

        function prompt
        {
            $(if (test-path variable:/PSDebugContext) { '[DBG]: ' } 
            else { '' }) + 'PS ' + $(Get-Location) `
            + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
        } 

此函數會使用 Test-Path Cmdlet 來判斷 $PSDebugContext 自動變數是否已填入。如果 $PSDebugContext 已填入、您處於偵錯模式且 "[DBG]" 已加入提示中時,如下:

        [DBG] PS C:\ps-test>

如果 $PSDebugContext 未被填入,則函式會將 "PS" 加入提示中。接著,函數會使用 Get-Location Cmdlet 來取得目前的檔案系統目錄位置。然後其會加入一個右角括弧 (>)。

    For example:        
        PS C:\ps-test>

如果您在巢狀提示中,則函式會將兩個角括弧 (>>) 加入提示中。(如果 $NestedPromptLevel 自動變數的值大於 1,您就會在巢狀提示中)。

例如,當您正在巢狀提示中偵錯,提示會與下列提示類似:

        [DBG] PS C:\ps-test>>>

對提示的變更

Enter-PSSession Cmdlet 會在目前 Prompt 函式前面加上遠端電腦名稱。當您使用 Enter-PSSession Cmdlet 搭配遠端電腦啟動工作階段時,命令提示字元會進行變更,以包含遠端電腦的名稱。例如:

          PS Hello, World> Enter-PSSession Server01
          [Server01]: PS Hello, World>

其他 Windows PowerShell 主應用程式和替代殼層可能會有自己的自訂命令提示字元。

如需有關 $PSDebugContext 和 $NestedPromptLevel 自動變數的詳細資訊,請參閱 about_Automatic_Variables。

如何自訂提示

若要自訂提示,請撰寫新的 Prompt 函式。該函式不受保護,因此您可以加以覆寫。

若要撰寫提示函式,請輸入下列命令:

        function prompt { }

然後在括號之間輸入命令或是輸入建立提示的字串。

例如,下列提示會包含您的電腦名稱:

        function prompt {"PS [$env:COMPUTERNAME]> "}

在 Server01 電腦上,該提示會類似於下列提示:

        PS [Server01] >

下列提示函式會包含目前的日期和時間:

        function prompt {"$(get-date)> "}

該提示會類似於下列提示:

        03/15/2012 17:49:47>

您也可以變更預設的 Prompt 函式:

例如,當 Windows PowerShell 透過使用 [以系統管理員身分執行] 選項而開啟時,下列修改後的 Prompt 函式會將 "[ADMIN]:" 加入內建的 Windows PowerShell 提示中。

        function prompt 
        {
            $identity = [Security.Principal.WindowsIdentity]::GetCurrent()
            $principal = [Security.Principal.WindowsPrincipal] $identity

            $(if (test-path variable:/PSDebugContext) { '[DBG]: ' } 

            elseif($principal.IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
            { "[ADMIN]: " }

            else { '' }) + 'PS ' + $(Get-Location) + $(if ($nestedpromptlevel -ge 1) { '>>' }) + '> '
        }

當您透過使用 [以系統管理員身分執行] 選項來啟動 Windows PowerShell 時,會顯示與下列提示類似的提示:

        [ADMIN]: PS C:\ps-test>

下列 Prompt 函式會顯示下一個命令的歷史記錄識別碼。若要檢視命令歷程記錄,請使用 Get-History Cmdlet。

          function prompt
          {
             # The at sign creates an array in case only one history item exists.
             $history = @(get-history)
             if($history.Count -gt 0)
             {
                $lastItem = $history[$history.Count - 1]
                $lastId = $lastItem.Id
             }

             $nextCommand = $lastId + 1
             $currentDirectory = get-location
             "PS: $nextCommand $currentDirectory >"
          }

下列提示會使用 Write-Host 和 Get-Random Cmdlet 來建立會隨機變更色彩的提示。由於 Write-Host 會寫入至目前的主應用程式但不會傳回物件,因此這個函式會包含 Return 陳述式。如果沒有,則 Windows PowerShell 會使用預設的提示 "PS>"。

        function prompt
        {
            $color = Get-Random -Min 1 -Max 16
            Write-Host ("PS " + $(Get-Location) +">") -NoNewLine -ForegroundColor $Color
            return " "
        }

儲存 PROMPT 函式

如同任何函數,Prompt 函式只存在於目前的工作階段。若要為未來的工作階段儲存 Prompt 函式,請將其加入您的 Windows PowerShell 設定檔中。如需有關設定檔的詳細資訊,請參閱 about_Profiles。

另請參閱

Get-Location

Enter-PSSession

Get-History

Get-Random

Write-Host

about_Profiles

about_Functions

about_Scopes

about_Debuggers

about_Automatic_Variables