
close
close
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.
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.
Set-PSReadlineOption -ContinuationPromptForegroundColor Magenta
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 "-->"
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
Most popular on petri