有默认的本地控制台颜色和设置,与 PSReadline 配置分开
'PowerShell rest console colors'
$host.PrivateData
<#
# Results
...
ErrorForegroundColor : #FFFF0000
ErrorBackgroundColor : #00FFFFFF
WarningForegroundColor : #FFFF8C00
WarningBackgroundColor : #00FFFFFF
VerboseForegroundColor : #FF00FFFF
VerboseBackgroundColor : #00FFFFFF
DebugForegroundColor : #FF00FFFF
DebugBackgroundColor : #00FFFFFF
ConsolePaneBackgroundColor : #FF012456
ConsolePaneTextBackgroundColor : #FF012456
ConsolePaneForegroundColor : #FFF5F5F5
ScriptPaneBackgroundColor : #FF1E1E1E
ScriptPaneForegroundColor : #FFD4D4D4
...
#>
[System.Enum]::GetValues('ConsoleColor')
<#
# Results
Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
#>
[System.Enum]::GetValues('ConsoleColor') |
ForEach-Object { Write-Host $_ -ForegroundColor $_ }
<#
# Results
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White
#>
# Setting console color
$host.PrivateData.ErrorBackgroundColor = "White"
重置控制台颜色 - Console.ResetColor Method
[Console]::ResetColor()
# Playing with colors
foreach($color1 in (0..15))
{
foreach($color2 in (0..15))
{Write-Host -ForegroundColor ([ConsoleColor]$color1) -BackgroundColor ([ConsoleColor]$color2) -Object "X" -NoNewline}
Write-Host
}
有人曾经说过:
Windows 10 上的 PowerShell 5.0 附带一个
增强且色彩丰富的 PowerShell 控制台。其他上的 PowerShell 5.0
操作系统只有乏味的标准控制台。
这是因为控制台增强功能来自一个名为
“PSReadLine”。如果您安装了 PowerShellGet(它始终是
PowerShell 5.0,可从 www.powershellgallery.com 下载
对于较旧的 PowerShell 版本),您可以下载此模块并打开
你的控制台也变成了一只五颜六色的野兽:
# download and install the module
Install-Module PSReadLine -Scope CurrentUser
# load the module to turn on the colors
# this could be done automatically in your $profile script
Import-Module PSReadline
但是,PSReadline 现在默认在 PowerShell 发行版中,无需下载。所以,根据您选择的配置有一个优先级问题。您可以禁用 PSReadline,以验证它是真正的 PSReadline 作为您的根案例还是 PowerShell 的默认值。
Set-PSReadLineOption
Set-PSReadLineOption [-EditMode ] [-ContinuationPrompt
] [-HistoryNoDuplicates] [-AddToHistoryHandler ]
[-HistorySearchCursorMovesToEnd] [-MaximumHistoryCount ]
[-MaximumKillRingCount ] [-ShowToolTips]
[-ExtraPromptLineCount ] [-DingTone ]
[-DingDuration ] [-BellStyle ]
[-CompletionQueryItems ] [-WordDelimiters ]
[-HistorySearchCaseSensitive] [-HistorySaveStyle
] [-HistorySavePath ]
[-AnsiEscapeTimeout ] [-PromptText ]
[-ViModeIndicator ] [-ViModeChangeHandler
] [-颜色] []