powershell - 无法更改控制台颜色

标签 powershell colors console

我曾尝试使用它们来自定义我的 PowerShell 控制台,但它们似乎都损坏了。 BackgroundColor 变黑,但只要我输入类似 dir 的内容,所有内容都会再次处于默认的蓝色背景中。我一直在做很多网络搜索,但找不到改变这一点的方法。您知道更改控制台以便我可以使 BackgroundColor 保持不变的方法吗?

$Shell = $Host.UI.RawUI
$Shell.BackgroundColor = "Black"
$Shell.ForegroundColor = "White"
$Shell.CursorSize = 10

此页面建议将这些值添加到 $profile 以使它们停留在控制台 session 中,但这不起作用。 https://4sysops.com/wiki/change-powershell-console-syntax-highlighting-colors-of-psreadline/

是否有其他一些 PSReadLine 函数(可能是 PS v5.1 选项)允许在 session 中永久固定控制台颜色?

* Set-PSReadLineOption options have changed
  - To specify colors, use the new `-Color` parameter and pass a Hashtable

https://www.powershellgallery.com/packages/PSReadLine/2.0.0-beta1/Content/Changes.txt

虽然没有关于如何改变背景颜色的例子......

最佳答案

有默认的 native 控制台颜色和设置,与 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"

Reset console colors - 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 
}

曾经有人说:

PowerShell 5.0 on Windows 10 ships with a much-enhanced and colorful PowerShell console. PowerShell 5.0 on other operating systems just has the dull standard console.

That's because the console enhancements come from a module called "PSReadLine". If you installed PowerShellGet (which is always part of PowerShell 5.0 and can be downloaded from www.powershellgallery.com for older PowerShell versions), you can download this module and turn your console into a colorful beast as well:

# 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 ] [-Colors ] []

关于powershell - 无法更改控制台颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60610211/

相关文章:

xml - PowerShell select-xml xpath 似乎不起作用

c# - PowerShell 字符串命令中的管道字符在 Csharp C# 代码中无法识别

Java swing - 频繁改变框架的颜色

Android 内边距颜色

python - Urwid 列表框 : How to get fluid focus movement?

php - Symfony OutputFormatterStyle "blink"不闪烁

.net - MySql DataTable的ForEach-Object循环第一行空白

SharePoint 2010,Powershell - 遍历所有文档库,创建 View 并将其设置为默认值

c# - 如何使用 C# 混合颜色 "naturally"?

jupyter-notebook - IPython REPL 任何地方 : how to share application context with IPython console for future interaction?