Powershell: Console Colors

A brief reminder for the topic of Console Colors and Powershell.

Get the current console/color setup

> $Host.Version.ToString()
5.1.19041.6093

> $Host.UI.RawUI.BackgroundColor
Black

> $Host.UI.RawUI.ForegroundColor
Gray

# Other Host colors which can be set by assining a different:
> $Host.PrivateData

ErrorForegroundColor    : Red
ErrorBackgroundColor    : Black
WarningForegroundColor  : Yellow
WarningBackgroundColor  : Black
DebugForegroundColor    : Yellow
DebugBackgroundColor    : Black
VerboseForegroundColor  : Yellow
VerboseBackgroundColor  : Black
ProgressForegroundColor : Yellow
ProgressBackgroundColor : DarkCyan

> Get-PSReadLineOption

EditMode                : Windows
# ...
VariableColor           : "$([char]0x1b)[92m"

Setting new colors

Next to the 16 predefined color names one can also use ANSI Escape Codes for defining a color theme – but those are a bit too complicated to explain it here in detail (see the Color entries of Get-PSReadLineOption for examples):

> [System.ConsoleColor].GetEnumNames()
Black
# ...

> Get-PSReadLineOption
# ...
SelectionColor : "$([char]0x1b)[30;47m"
StringColor    : "$([char]0x1b)[36m"
# ...

Example

The following changes it will only be active in the current Powershell session!
To change the colors permanently, put it into your Powershell profile.

$Host.UI.RawUI.BackgroundColor = 'Black'
$Host.UI.RawUI.ForegroundColor = 'Gray'

$Host.PrivateData.ErrorForegroundColor = 'Red'
# ...

$NewColorTheme = @{
    CommandColor             = "$([char]0x1b)[93m"
    ...
    SelectionColor           = "$([char]0x1b)[30;47m"
    StringColor              = "$([char]0x1b)[36m"
    TypeColor                = "$([char]0x1b)[37m"
    VariableColor            = "$([char]0x1b)[92m"
}

Set-PSReadLineOption -Colors $NewColorTheme