about_Prompts

簡短描述

描述 函式 Prompt ,並示範如何建立自定義 Prompt 函式。

詳細描述

PowerShell 命令提示字元指出 PowerShell 已準備好執行命令:

PS C:\>

PowerShell 具有內 Prompt 建函式。 您可以在 PowerShell 設定檔文稿中定義自己的自訂 Prompt 函式。

關於 Prompt 函式

Prompt 式會決定PowerShell提示字元的外觀。 PowerShell 隨附內 Prompt 建函式,但您可以定義自己的 Prompt 函式來覆寫它。

Prompt 式具有下列語法:

function Prompt { <function-body> }

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

例如,下列 Prompt 函式會傳回 「Hello, World」 字串,後面接著右角括號 (>)。

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

取得提示函式

若要取得函 Prompt 式,請使用 Get-Command Cmdlet 或使用 Get-Item 函式磁碟驅動器中的 Cmdlet。

例如:

PS C:\> Get-Command Prompt

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

若要取得設定提示值的腳本,請使用 dot 方法來取得函式的 Prompt ScriptBlock 屬性。

例如:

(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 ,才會顯示預設提示。

預設 PowerShell 提示字元為:

PS>

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

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

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

內建提示

PowerShell 包含內 Prompt 建函式。

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

函式會 Test-Path 使用 Cmdlet 來測試自動變數是否有 $PSDebugContext 值。 如果有 $PSDebugContext 值,您正在偵錯模式中執行,而且 [DBG]: 會新增至提示,如下所示:

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

如果未 $PSDebugContext 填入 ,函式會將 新增 PS 至提示。 而且,函式會 Get-Location 使用 Cmdlet 來取得目前的檔案系統目錄位置。 然後,它會新增右角括弧 (>)。

例如:

PS C:\ps-test>

如果您是在巢狀提示字元中,函式會將兩個角括號 (>>) 新增至提示。 如果自動變數的值 $NestedPromptLevel 大於 0,您就會在巢狀提示中。

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

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

提示的變更

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

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

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

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

如何自定義提示

若要自定義提示,請撰寫新的 Prompt 函式。 函式未受到保護,因此您可以覆寫它。

若要撰寫函 Prompt 式,請輸入下列專案:

function prompt { }

然後,在大括弧之間,輸入命令或建立提示的字串。

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

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

在 Server01 計算機上,提示類似下列提示:

PS [Server01] >

下列 Prompt 函式包含目前的日期和時間:

function prompt {"$(Get-Date)> "}

提示類似下列提示:

03/15/2012 17:49:47>

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

例如,下列修改過的 Prompt 函式會在提升許可權的會話中執行時,新增 [ADMIN]: 至內建 PowerShell 提示字元。

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

  $(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' }
    elseif($principal.IsInRole($adminRole)) { "[ADMIN]: " }
    else { '' }
  ) + 'PS ' + $(Get-Location) +
    $(if ($NestedPromptLevel -ge 1) { '>>' }) + '> '
}

當您使用 [ 以系統管理員 身分執行] 選項啟動 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-HostGet-Random Cmdlet 來建立隨機變更色彩的提示。 由於 Write-Host 寫入目前主應用程式但不會傳回 物件,因此此函式包含 Return 語句。 如果沒有它,PowerShell 會使用預設提示 。 PS>

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

儲存提示函式

如同任何函式,函 Prompt 式只存在於目前的會話中。 若要儲存 Prompt 函式以供未來的會話使用,請將它新增至 PowerShell 配置檔。 如需配置檔的詳細資訊,請參閱 about_Profiles

另請參閱