Color Your World with PSReadLine -- Part 3
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.
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.
Because PSReadline is cross platform, you can also use it in Linux.
As with Windows, you can check the current configuration, although settings may vary.
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.
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
You can experiment with different values as much as you want. You can reset most colors like this:
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 "-->"
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.
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.
More in PowerShell
Use PowerShell Functions to Quickly Simplify Your Scripts
Apr 17, 2023 | Bill Kindle
Microsoft Updates Teams PowerShell Module to Version 5.0
Mar 6, 2023 | Rabia Noureen
How to Create Conditional Access Policies using PowerShell
Jan 4, 2023 | Liam Cleary
Microsoft Releases PowerShell Crescendo 1.1.0-preview01
Dec 8, 2022 | Rabia Noureen
How To Set Environment Variables With PowerShell
Dec 5, 2022 | Sukesh Mudrakola
How to Use a PowerShell Foreach Loop
Oct 24, 2022 | Ivan Mirchev
Most popular on petri