无法更改控制台颜色

时间:2020-03-10 00:48:37

标签: powershell colors console

我曾尝试使用它们来自定义我的PowerShell控制台,但是它们似乎都坏了。 BackgroundColor变黑,但是一旦我输入dir之类的东西,所有内容都会再次处于默认的蓝色背景中。我已经进行了很多网络搜索,但是找不到改变它的方法。您知道一种更改控制台的方法,以便让BackgroundColor保持住状态吗?

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

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

是否还有其他PSReadLine函数(可能是PS v5.1选项)可以永久修复会话中的控制台颜色?

* 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

没有关于如何更改背景颜色的示例...

1 个答案:

答案 0 :(得分:0)

有默认的本机控制台颜色和设置,与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,以验证它是根目录还是PowerShell的默认PSReadline。

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 ] [-颜色] []