PowerShell: How to use Color in Prompt, Console, and Output Message

Bruce Wen
5 min readJul 27, 2021

Most Linux developers like to use customized prompt — OK, they just love it and probably also want to make the boring programming life a bit fun. So, how to do the same for PowerShell developers in Windows? This article will tell you details about color usage in Powershell Prompt, Console, and Output Message.

Photo by Denise Chan on Unsplash

How to set colorful prompt?

You copy this function into your powershell profile: (E.g. C:\Users\wenijinew\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 — Check more on about_Profiles)

Gist 1: Function to set Powershell Prompt

Then, you will get prompt like this:

Figure 1: Customized Colorful[ads] Powershell Prompt

You can add more information in prompt. For example, current time:

Gist 2: Powershell Get Date with Format

Then, the function will look like:

Gist 3: Powershell set Colorful Prompt with Time

And prompt will be:

Figure 2: Powershell Prompt with Time

More about Color in Powershell

Powershell has configuration options for foreground and background color of Error, Warning, Debug, Verbose, Progress in console by $Host.PrivateData:

> $Host.PrivateDataErrorForegroundColor    : Magenta
ErrorBackgroundColor : Black
WarningForegroundColor : Yellow
WarningBackgroundColor : Black
DebugForegroundColor : Yellow
DebugBackgroundColor : Black
VerboseForegroundColor : Yellow
VerboseBackgroundColor : Black
ProgressForegroundColor : Yellow
ProgressBackgroundColor : DarkCyan

To change the color of above properties, assign new color name directly. For example,

$Host.PrivateData.ErrorForegroundColor = "Red"
$Host.PrivateData.ErrorBackgroundColor = "Yellow"
Figure 3: Changed Error Foreground Color to Red and Background Color to Yellow

The value of the color cannot be RGB code like #6ff542 :

egugwen@SE-00006414(ps) 17:05:24> $Host.PrivateData.ErrorForegroundColor = "#6ff542"                                                                                                        Exception setting "ErrorForegroundColor": "Cannot convert value "#6ff542" to type "System.ConsoleColor". Error: "Unable to match the identifier name #6ff542 to a valid enumerator name. Sp ecify one of the following enumerator names and try again:                                                                                                                                  Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, White""                                          
At line:1 char:1 + $Host.PrivateData.ErrorForegroundColor = "#6ff542" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException + FullyQualifiedErrorId : ExceptionWhenSetting

When we check more information about System.ConsoleColor , we can get to know how the color is used by Write-Host :

> Write-Host "Red on white text." -ForegroundColor red -BackgroundColor white                                                                               Red on white text.
Figure 4: Write-Host to Control Output Color in Console

Check ConsoleColor web page for more details.

Another Powershell configuration about color is Get-PSReadLineOption :

> Get-PSReadLineOptionEditMode                               : Windows
AddToHistoryHandler :
HistoryNoDuplicates : True
HistorySavePath : C:\Users\egugwen\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt
HistorySaveStyle : SaveIncrementally
HistorySearchCaseSensitive : False
HistorySearchCursorMovesToEnd : False
MaximumHistoryCount : 4096
ContinuationPrompt : >>
PromptText :
BellStyle : Audible
DingDuration : 50
DingTone : 1221
CommandsToValidateScriptBlockArguments : {ForEach-Object, %, Invoke-Command, icm...}
CommandValidationHandler :
MaximumKillRingCount : 10
ShowToolTips : True
ViModeIndicator : None WordDelimiters : ;:,.[]{}()/\|^&*-=+'"–—― CommandColor : "$([char]0x1b)[93m" CommentColor : "$([char]0x1b)[32m" ContinuationPromptColor : "$([char]0x1b)[37m" DefaultTokenColor : "$([char]0x1b)[37m" ErrorColor : "$([char]0x1b)[91m" KeywordColor : "$([char]0x1b)[92m" MemberColor : "$([char]0x1b)[97m" NumberColor : "$([char]0x1b)[97m" OperatorColor : "$([char]0x1b)[90m" ParameterColor : "$([char]0x1b)[90m" SelectionColor : "$([char]0x1b)[30;47m" StringColor : "$([char]0x1b)[36m" TypeColor : "$([char]0x1b)[37m" VariableColor : "$([char]0x1b)[92m"

The above options can be changed by command Set-PSReadLineOption , for example, set more than one color value in a single command:

Set-PSReadLineOption -Colors @{
Command = 'Magenta'
Number = 'DarkGray'
Member = 'DarkGray'
Operator = 'DarkGray'
Type = 'DarkGray'
Variable = 'DarkGreen'
Parameter = 'DarkGreen'
ContinuationPrompt = 'DarkGray'
Default = 'DarkGray'
}

The value of the color properties can be text value of ConsoleColor or 24 bit color escape sequence or RGB value:

Set-PSReadLineOption -Colors @{
# Use a ConsoleColor enum
"Error" = [ConsoleColor]::DarkRed
# 24 bit color escape sequence
"String" = "$([char]0x1b)[38;5;100m"
# RGB value
"Command" = "#8181f7"
}

To print messages in specified color

By using 24 bit color escape sequence, we can control the color of the message printed in console:

Gist 5: Function to Print message in color
Figure 5: Customized color for console output by “24 Bit Color Escape Sequence”

To reuse the code to color specified message, we can define function to do this. For example, below function is to set color of $msg to yellow.

Gist 6: Function to print message in Yellow and Green
Figure 3: Use function to set message color in console

By using color, we can highlight those information we want to take care. For example, used and free disk space: (read “PowerShell: Get Human-Readable Disk Space Size” to know more)

Figure 6: Highligh Used and Free Disk Space by Different Color

That’s all about Powershell color usage in prompt, console and message printed by echo. Wish it helpful for you in Powershell programming.

Thanks for reading and happy coding!

References

Update for PowerShell 7.2+

PowerShell 7.2 added a new automatic variable, $PSStyle, and changes to the PowerShell engine to support the output of ANSI-decorated text.

For example, by default, in PowerShell 7.2+ version, directories are showed as below:

Because the definition for FileInfo.Directory is “`e[44;1m”. When we change it $PSStyle.FileInfo.Directory=”`e[34;1m” , directories will be shown as:

And we can get all default or current settings for $PSStyle

> $PSStyle

Reset : `e[0m
BlinkOff : `e[25m
Blink : `e[5m
BoldOff : `e[22m
Bold : `e[1m
Hidden : `e[8m
HiddenOff : `e[28m
Reverse : `e[7m
ReverseOff : `e[27m
ItalicOff : `e[23m
Italic : `e[3m
UnderlineOff : `e[24m
Underline : `e[4m
StrikethroughOff : `e[29m
Strikethrough : `e[9m
OutputRendering : Host
Formatting.FormatAccent : `e[32;1m
Formatting.TableHeader : `e[32;1m
Formatting.ErrorAccent : `e[36;1m
Formatting.Error : `e[31;1m
Formatting.Warning : `e[33;1m
Formatting.Verbose : `e[33;1m
Formatting.Debug : `e[33;1m
Progress.Style : `e[33;1m
Progress.MaxWidth : 120
Progress.View : Minimal
Progress.UseOSCIndicator : False
FileInfo.Directory : `e[44;1m
FileInfo.SymbolicLink : `e[36;1m
FileInfo.Executable : `e[32;1m
FileInfo.Extension : .zip,.tgz,.gz,.tar,.nupkg,.cab,.7z,.ps1,.psd1,.psm1,.ps1xml
Foreground.Black : `e[30m
Foreground.BrightBlack : `e[90m
Foreground.White : `e[37m
Foreground.BrightWhite : `e[97m
Foreground.Red : `e[31m
Foreground.BrightRed : `e[91m
Foreground.Magenta : `e[35m
Foreground.BrightMagenta : `e[95m
Foreground.Blue : `e[34m
Foreground.BrightBlue : `e[94m
Foreground.Cyan : `e[36m
Foreground.BrightCyan : `e[96m
Foreground.Green : `e[32m
Foreground.BrightGreen : `e[92m
Foreground.Yellow : `e[33m
Foreground.BrightYellow : `e[93m
Background.Black : `e[40m
Background.BrightBlack : `e[100m
Background.White : `e[47m
Background.BrightWhite : `e[107m
Background.Red : `e[41m
Background.BrightRed : `e[101m
Background.Magenta : `e[45m
Background.BrightMagenta : `e[105m
Background.Blue : `e[44m
Background.BrightBlue : `e[104m
Background.Cyan : `e[46m
Background.BrightCyan : `e[106m
Background.Green : `e[42m
Background.BrightGreen : `e[102m
Background.Yellow : `e[43m
Background.BrightYellow : `e[103m

That’s all! Thanks for reading and stay tuned.

--

--