Cannot change console colors

Viewed 537

I have tried to use these to customise my PowerShell console, but they all seem broken. The BackgroundColor goes black, but as soon as I type something like dir everything is in the default blue background again. I've been doing quite a few web searches and cannot find a way to just change this. Do you know a way to change the console so that I can make the BackgroundColor hold?

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

This page suggests adding these values to $profile to make them stick in the console session, but this does not work. https://4sysops.com/wiki/change-powershell-console-syntax-highlighting-colors-of-psreadline/

Is there some other PSReadLine function (possibly a PS v5.1 option) that does allow permanently fixing the console colors in a 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

No example on how to change backgroundcolor though...

1 Answers

There is the default native console colors and settings, which is separate from PSReadline configs

'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 
}

Someone once said:

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

However, PSReadline is now in PowerShell distributions by default, no download needed. SO, there is a matter of precedence based on the config you choose. You can disable PSReadline, to validate if it is truly PSReadline as your root case or the default of 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 ] []

Related