How to Change Colors in PowerShell

  1. Introduction to Changing Colors in PowerShell
  2. Use the [System.Enum] Class in PowerShell
  3. Changing the Console Color in PowerShell
How to Change Colors in PowerShell

This article will discuss how to change font colors, the background color of scripts, and the console window color using PowerShell.

Introduction to Changing Colors in PowerShell

These commands retrieve an object with information about the PowerShell console, the console host.

Command:

$host

Output:

Name             : Windows PowerShell ISE Host
Version          : 5.1.22000.282
InstanceId       : 8cff2bea-868b-4d9e-b55a-06a3f4b8c20c
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-PH
CurrentUICulture : en-US
PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

The PrivateData property has all the color properties we are looking for.

Command:

$host.PrivateData

Output:

ErrorForegroundColor                      : #FFFF9494
ErrorBackgroundColor                      : #00FFFFFF
WarningForegroundColor                    : #FFFF8C00
WarningBackgroundColor                    : #00FFFFFF
VerboseForegroundColor                    : #FF00FFFF
VerboseBackgroundColor                    : #00FFFFFF
DebugForegroundColor                      : #FF00FFFF
DebugBackgroundColor                      : #00FFFFFF
ConsolePaneBackgroundColor                : #FF012456
ConsolePaneTextBackgroundColor            : #FF012456
ConsolePaneForegroundColor                : #FFF5F5F5
ScriptPaneBackgroundColor                 : #FFFFFFFF
ScriptPaneForegroundColor                 : #FF000000

The colors are set for the Warnings, Errors, Debug, Verbose, and Progress streams inside the $host.PrivateData object. Try changing one of these values and seeing if your console also changes colors.

Command:

$host.PrivateData.ErrorBackgroundColor = "White"

The error background default color "black" will change to "white".

changed error background color

Use the [System.Enum] Class in PowerShell

To query for all the console colors in PowerShell, we run the command below.

Command:

[System.Enum]::GetValues('ConsoleColor')

Output:

Black
DarkBlue
DarkGreen
DarkCyan
DarkRed
DarkMagenta
DarkYellow
Gray
DarkGray
Blue
Green
Cyan
Red
Magenta
Yellow
White

The displayed output on the console is plain white text (if default colors have not been changed). Do the following command to display the console colors as their respective colors.

Command:

[System.Enum]::GetValues('ConsoleColor') |
    ForEach-Object { Write-Host $_ -ForegroundColor $_ }

Output:

Displaying color values with font colors using Write-Host

Suppose we wanted to see which color combinations would work and suit our preference. In that case, we can use the command below that will output all the possible foreground colors on all possible background colors.

Command:

$colors = [enum]::GetValues([System.ConsoleColor])
Foreach ($bgcolor in $colors) {
    Foreach ($fgcolor in $colors) {
        Write-Host "$fgcolor|"  -ForegroundColor $fgcolor -BackgroundColor $bgcolor -NoNewline
    }
    Write-Host " on $bgcolor"
}

Output:

Displaying all possible combinations of foreground and background console colors

Changing the Console Color in PowerShell

Changing the prominent console foreground and the background color is slightly different from our previous object. We may try the snippet below to change the text color and the console window of PowerShell.

Command:

$host.UI.RawUI.ForegroundColor = "DarkGreen"
$host.UI.RawUI.BackgroundColor = "Black"
cls

Output:

Changing the Console Color in PowerShell

Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell Color