PowerShell 和 PSReadLine 的預設色彩是為深色背景終端機所選擇的。 不過,有些使用者可能會選擇使用淺色背景搭配深色文字。 由於大部分的預設色彩不會設定背景,因此在淺色背景上使用淺色前景色彩會產生無法讀取的文字。
從 PowerShell 7.2 開始,PowerShell 會將彩色輸出新增至預設控制台體驗。 此功能 $PSStyle 在 Windows PowerShell 中並非原生提供。 不過,使用 PowerShell 圖庫中的 PSStyle 模組,你可以用本文描述的這些技術來設定顏色值。
所使用的色彩會定義在變數中 $PSStyle ,並針對深色背景而設計。 你可以調整這些顏色,以更好地適應淺色背景的終端機。
PSReadLine 可讓您定義 18 個不同語法元素的色彩。 您可以使用 Cmdlet 來檢視目前的設定 Get-PSReadLineOption 。
EditMode : Windows
AddToHistoryHandler : System.Func`2[System.String,System.Object]
HistoryNoDuplicates : True
HistorySavePath : C:\Users\user1\AppData\Roaming\Microsoft\Wind...
HistorySaveStyle : SaveIncrementally
HistorySearchCaseSensitive : False
HistorySearchCursorMovesToEnd : False
MaximumHistoryCount : 4096
ContinuationPrompt : >>
ExtraPromptLineCount : 0
PromptText : {> }
BellStyle : Audible
DingDuration : 50
DingTone : 1221
CommandsToValidateScriptBlockArguments : {ForEach-Object, %, Invoke-Command, icm...}
CommandValidationHandler :
CompletionQueryItems : 100
MaximumKillRingCount : 10
ShowToolTips : True
ViModeIndicator : None
WordDelimiters : ;:,.[]{}()/\|^&*-=+'"-—―
AnsiEscapeTimeout : 100
PredictionSource : HistoryAndPlugin
PredictionViewStyle : InlineView
CommandColor : "`e[93m"
CommentColor : "`e[32m"
ContinuationPromptColor : "`e[37m"
DefaultTokenColor : "`e[37m"
EmphasisColor : "`e[96m"
ErrorColor : "`e[91m"
InlinePredictionColor : "`e[38;5;238m"
KeywordColor : "`e[92m"
ListPredictionColor : "`e[33m"
ListPredictionSelectedColor : "`e[48;5;238m"
MemberColor : "`e[97m"
NumberColor : "`e[97m"
OperatorColor : "`e[90m"
ParameterColor : "`e[90m"
SelectionColor : "`e[30;47m"
StringColor : "`e[36m"
TypeColor : "`e[37m"
VariableColor : "`e[92m"
色彩設定會儲存為字串,其中包含變更終端機色彩的 ANSI 逸出序列。 使用 Set-PSReadLineOption cmdlet,您可以將色彩變更為更適合淺色背景的值。
為淺色主題定義色彩
PowerShell ISE 可以設定為針對編輯器和控制檯窗格使用淺色主題。 您也可以檢視和變更 ISE 用於各種語法和輸出類型的色彩。 您可以使用這些色彩選項來定義 PSReadLine 的類似主題。
下列哈希表會定義 PSReadLine 的色彩,以模擬 PowerShell ISE 中的色彩。
$ISETheme = @{
Command = $PSStyle.Foreground.FromRGB(0x0000FF)
Comment = $PSStyle.Foreground.FromRGB(0x006400)
ContinuationPrompt = $PSStyle.Foreground.FromRGB(0x0000FF)
Default = $PSStyle.Foreground.FromRGB(0x0000FF)
Emphasis = $PSStyle.Foreground.FromRGB(0x287BF0)
Error = $PSStyle.Foreground.FromRGB(0xE50000)
InlinePrediction = $PSStyle.Foreground.FromRGB(0x93A1A1)
Keyword = $PSStyle.Foreground.FromRGB(0x00008b)
ListPrediction = $PSStyle.Foreground.FromRGB(0x06DE00)
Member = $PSStyle.Foreground.FromRGB(0x000000)
Number = $PSStyle.Foreground.FromRGB(0x800080)
Operator = $PSStyle.Foreground.FromRGB(0x757575)
Parameter = $PSStyle.Foreground.FromRGB(0x000080)
String = $PSStyle.Foreground.FromRGB(0x8b0000)
Type = $PSStyle.Foreground.FromRGB(0x008080)
Variable = $PSStyle.Foreground.FromRGB(0xff4500)
ListPredictionSelected = $PSStyle.Background.FromRGB(0x93A1A1)
Selection = $PSStyle.Background.FromRGB(0x00BFFF)
}
備註
你可以用這個 FromRGB() 方法來建立你想要顏色的 ANSI 跳脫序列。
如需 的詳細資訊 $PSStyle,請參閱 about_ANSI_Terminals。 如需 ANSI 逸出序列的詳細資訊,請參閱維琪百科中的 ANSI 逸出程式碼 一文。
在個人資料中設定色彩主題
若要讓每個 PowerShell 工作階段中的顏色設定,您必須將設定新增至 PowerShell 設定檔案腳本。 如需範例,請參閱 自訂Shell環境
將 $ISETheme 變數和下列 Set-PSReadLineOption 命令新增至您的配置檔。
Set-PSReadLineOption -Colors $ISETheme
下列設定適用於淺色背景終端機。
$PSStyle.Formatting.FormatAccent = $PSStyle.Foreground.Green
$PSStyle.Formatting.TableHeader = $PSStyle.Foreground.Green
$PSStyle.Formatting.ErrorAccent = $PSStyle.Foreground.Cyan
$PSStyle.Formatting.Error = $PSStyle.Foreground.Red
$PSStyle.Formatting.Warning = $PSStyle.Foreground.Yellow
$PSStyle.Formatting.Verbose = $PSStyle.Foreground.Yellow
$PSStyle.Formatting.Debug = $PSStyle.Foreground.Yellow
$PSStyle.Progress.Style = $PSStyle.Foreground.Yellow
$PSStyle.FileInfo.Directory = $PSStyle.Background.FromRgb(0x2f6aff) +
$PSStyle.Foreground.BrightWhite
$PSStyle.FileInfo.SymbolicLink = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Executable = $PSStyle.Foreground.BrightMagenta
$PSStyle.FileInfo.Extension['.ps1'] = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Extension['.ps1xml'] = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Extension['.psd1'] = $PSStyle.Foreground.Cyan
$PSStyle.FileInfo.Extension['.psm1'] = $PSStyle.Foreground.Cyan
選擇無障礙功能的色彩
ISE色彩主題可能不適合色盲或其他限制看色能力的使用者。
萬維網聯合會 (W3C) 有使用輔助功能色彩的建議。 Web 內容輔助功能指導方針 (WCAG) 2.1 建議「文字和影像的視覺呈現與文字的對比比例至少為 4.5:1」。如需詳細資訊,請參閱成功準則 1.4.3 對比(最小值)。
對比比例網站提供一個工具,可讓您挑選前景和背景色彩,並測量對比。 您可以使用此工具來尋找最適合您的色彩組合。