PowerShell と PSReadLine の両方の既定の色は、暗い背景ターミナルに対して選択されます。 ただし、一部のユーザーは、暗いテキストで明るい背景を使用することを選択する場合があります。 既定の色のほとんどは背景を設定しないため、明るい背景に明るい前景色を使用すると、読み取り不可能なテキストが生成されます。
PowerShell 7.2 以降、PowerShell は色分けされた出力を既定のコンソール エクスペリエンスに追加します。
$PSStyle機能は、Windows PowerShell ではネイティブに使用できません。 ただし、PowerShell ギャラリーの PSStyle モジュールを使用すると、この記事で説明するのと同じ手法を使用して色の値を設定できます。
使用される色は、 $PSStyle 変数で定義され、暗い背景用に設計されています。 これらの色は、明るい背景ターミナルに適するように変更できます。
PSReadLine を使用すると、18 種類の構文要素の色を定義できます。
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 コマンドレットを使用すると、明るい色の背景に適した値に色を変更できます。
ライト テーマの色の定義
PowerShell ISE は、エディター ウィンドウとコンソール ウィンドウの両方にライト テーマを使用するように構成できます。 ISE がさまざまな構文と出力の種類に使用する色を表示および変更することもできます。 これらの色の選択を使用して、 PSReadLine の同様のテーマを定義できます。
次のハッシュテーブルは、PowerShell ISE の色を模倣する PSReadLine の色を定義します。
$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 エスケープ シーケンスの詳細については、Wikipedia の ANSI エスケープ コード 記事を参照してください。
プロファイルでカラー テーマを設定する
すべての PowerShell セッションで必要な色の設定を行うには、構成設定を PowerShell プロファイル スクリプトに追加する必要があります。 例については、シェル環境のカスタマイズを参照してください。
$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 カラー テーマは、色覚障瞝のあるユーザーや、色を表示する能力を制限するその他の条件を持つユーザーには機能しない可能性があります。
World Wide Web Consortium (W3C) には、アクセシビリティのために色を使用するための推奨事項があります。 Web コンテンツ アクセシビリティ ガイドライン (WCAG) 2.1 では、"テキストとテキストの画像の視覚的な表示は、少なくとも 4.5:1 のコントラスト比を持つ" ことをお勧めします。詳細については、「 成功条件 1.4.3 コントラスト (最小)」を参照してください。
コントラスト比 Web サイトには、前景色と背景色を選択してコントラストを測定できるツールが用意されています。 このツールを使用すると、最適な色の組み合わせを見つけることができます。
PowerShell