【问题标题】:Cannot change console colors无法更改控制台颜色
【发布时间】:2020-03-10 00:48:37
【问题描述】:

我曾尝试使用这些来自定义我的 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

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

【问题讨论】:

    标签: powershell colors console


    【解决方案1】:

    有默认的本地控制台颜色和设置,与 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 ] [-颜色] []

    【讨论】:

      猜你喜欢
      • 2016-03-04
      • 2013-06-27
      • 2015-05-01
      • 1970-01-01
      • 2011-08-11
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      相关资源
      最近更新 更多