關於提示字元
簡短描述
描述函式 Prompt
,並示範如何建立自訂 Prompt
函數。
完整描述
PowerShell 命令提示字元表示 PowerShell 已準備好執行命令:
PS C:\>
PowerShell 提示字元是由內建函數所決定 Prompt
。 您可以建立自己的函 Prompt
式,並將它儲存在您的 PowerShell 設定檔中,以自訂提示。
關於提示字元函式
函式會 Prompt
決定 PowerShell 提示字元的外觀。
PowerShell 隨附內建 Prompt
函數,但是您可以藉由定義自己的函式來覆寫它 Prompt
。
Prompt
函數具有下列語法:
function Prompt { <function-body> }
函 Prompt
式必須傳回物件。 最佳做法是傳回字串或格式化為字串的物件。 建議的最大長度為 80 個字元。
例如,下列函式會傳回 Prompt
"Hello,World" 字串,後面接著右角括弧( >
)。
PS C:\> function prompt {"Hello, World > "}
Hello, World >
取得 Prompt 函式
若要取得函式 Prompt
,請使用 Get-Command
Cmdlet,或在 Get-Item
函數磁片磁碟機中使用 Cmdlet。
例如:
PS C:\> Get-Command Prompt
CommandType Name ModuleName
----------- ---- ----------
Function prompt
若要取得設定提示值的腳本,請使用點方法來取得函數的ScriptBlock屬性 Prompt
。
例如:
(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 {
$(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 來取得目前的檔案系統目錄位置。 然後,它會加入右角括弧( >
)。
例如:
PS C:\ps-test>
如果您是在嵌套的提示字元中,函式會將兩個角括弧( >>
)新增至提示字元。 (如果自動變數的值大於1,您就會在嵌套的提示字元中 $NestedPromptLevel
)。
例如,當您在巢狀的提示字元中偵錯時,提示字元會類似下列提示字元︰
[DBG] PS C:\ps-test>>>
提示的變更
Cmdlet 會在目前的函式前面加上 Enter-PSSession
遠端電腦的名稱 Prompt
。 當您使用 Enter-PSSession
Cmdlet 來啟動遠端電腦的會話時,命令提示字元會變更為包含遠端電腦的名稱。 例如:
PS Hello, World> Enter-PSSession Server01
[Server01]: PS Hello, World>
其他 PowerShell 主應用程式和替代 shell 可能會有自己的自訂命令提示字元。
如需 $PSDebugContext
和自動變數的詳細資訊 $NestedPromptLevel
,請參閱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 時,新增至內建的 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) { '>>' }) + '> '
}
當您使用 [以系統管理員身分執行] 選項啟動 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
語句。 如果沒有它,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。