Color Your World with PSReadLine — Part 3

PowerShell Text Purple hero
Over the course of the last few articles, I have been introducing you to the features and benefits of PSReadLiine. If you missed the previous articles, I encourage you to get caught up or you might feel a little lost today.

One of the real benefits of PSReadline is real-time colorized syntax in the PowerShell console. You have seen something similar when creating scripts in the PowerShell ISE. But now, you can get the same type of colorized highlighting in a PowerShell console.

Colorized commands with PSReadline (Image Credit: Jeff Hicks)
Colorized Commands with PSReadline (Image Credit: Jeff Hicks)

Different elements in a PowerShell expression, such as strings, operators, and commands are displayed in different colors. PSReadline has a default color configuration for these elements which you can see with Get-PSReadlineOption.
Default token colors in Windows (Image Credit: Jeff Hicks)
Default Token Colors in Windows (Image Credit: Jeff Hicks)

Because PSReadline is cross platform, you can also use it in Linux.
PSReadline in Ubuntu (Image Credit: Jeff Hicks)
PSReadline in Ubuntu (Image Credit: Jeff Hicks)

As with Windows, you can check the current configuration, although settings may vary.
PSReadline color options on Linux (Image Credit: Jeff Hicks)
PSReadline Color Options on Linux (Image Credit: Jeff Hicks)

This is nice and everything, especially if you stick with default settings for your console. But sometimes you might need to make a change.

For example, in Windows the color for a string (e.g. text) is DarkCyan. That may be hard to read for some people. Thanks to my buddy Shane Young who brought this to my attention. Fortunately, it is not too difficult to modify using Set-PSReadlineOption. The tricky part is understanding the syntax.

Set-PSReadlineOption -TokenKind String -ForegroundColor Cyan

You need to specify the TokenKind, which will be something like String, Comment, Variable, or Operator. Then you can specify a foreground and background color. The color must be one of the console colors, such as what you would use with Write-Host.
Notice now how much easier it is to read string elements.

Modified string foregroundcolor (Image Credit: Jeff Hicks)
Modified String Foregroundcolor (Image Credit: Jeff Hicks)

Some elements though are not listed as tokens. I do not mind the >> characters to indicate command continuation (although I could change it with PSReadline) but I would like these characters to pop out a bit more. Let’s make them Magenta.

Set-PSReadlineOption -ContinuationPromptForegroundColor Magenta

Modified continuation character color (Image Credit: Jeff Hicks)
You can experiment with different values as much as you want. You can reset most colors like this:

Set-PSReadlineOption -ResetTokenColors

Be aware that this will only reset colors that were modified with Set-PSReadLineOption and the -Token parameter. Other color-related settings like the ContinuationPrompt color are not reset. You would need to re-run the set command. As a last resort, you can exit and restart your PowerShell session as any changes you made are not persistent. If you know you always want a specific color setting, put the Set-PSReadlineOption commands in your PowerShell profile script. Here is what I have been trying out lately in my profile:

Set-PSReadlineOption -TokenKind String -ForegroundColor Cyan
Set-PSReadlineOption -ContinuationPromptForegroundColor white -ContinuationPromptBackgroundColor magenta -ContinuationPrompt "-->"

My PSReadline color scheme in action (Image Credit: Jeff Hicks)
My PSReadline Color Scheme in Action (Image Credit: Jeff Hicks)

Before we leave this topic, I want to mention something about the Error foreground and background color setting options you will see with Set-PSReadlineOption. These have nothing to do with changing the colors of your error messages, most likely red on black. These values can be set under $Host.PrivateData. Instead, PSReadline is doing something much more subtle.
As you type a command, PSReadline is parsing and analyzing it behind the scenes. If it detects an error, the > character in your prompt will change to the defined error color.
A PSReadline error indication (Image Credit: Jeff Hicks)
A PSReadline Error Indication (Image Credit: Jeff Hicks)

If you are not paying close attention, you may miss the color change. If you prefer to modify this, you should be able to run:

Set-PSReadlineOption -ErrorForegroundColor white -ErrorBackgroundColor red

But as of version 1.2 of the PSReadline module, this will run but be ignored. I would expect to see a white > on a red background when an error is detected but that doesn’t happen. I have filed a bug and requested a bit more information.

Next time, we will jump into the very cool parts of PSReadline. This is where the more I use, the more I realize how dramatically it will change the way I work.