Light Theme for the Powershell Console

Whenever I give presentations that include Visual Studio I usually change my theme from the dark to the light theme to improve the contrast of the presentation for those viewing a projector/screen.  Visual Studio has made this VERY easy which is nice. 

Recently I have been showing more and more output from the PowerShell console and thought it would be nice to do the same there as well.  Although there have been some big improvements to the console host in Windows 10, I found changing to a lighter theme was not easy.  I found several blog posts that showed how to change certain colors in the console programmatically but none seemed to fit my needs.

After learning from several of these posts I developed my own functions for inverting the console colors to a lighter theme and then resetting them back to the defaults.


Function invert-consolecolors {

    $oldForeground = $host.ui.rawui.ForegroundColor 
    $oldBackground = $host.ui.rawui.BackgroundColor
    $host.ui.rawui.ForegroundColor = $oldBackground
    $host.ui.rawui.BackgroundColor = $oldForeground

    Set-PSReadlineOption -ContinuationPromptBackgroundColor $oldForeground
    Set-PSReadlineOption -Token None -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Comment -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Keyword -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token String -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Operator -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Variable -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Command -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Parameter -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Type -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Number -BackgroundColor $oldForeground
    Set-PSReadlineOption -Token Member -BackgroundColor $oldForeground
    Set-PSReadlineOption -EmphasisBackgroundColor $oldForeground
    Set-PSReadlineOption -ErrorBackgroundColor $oldForeground

    Set-PSReadlineOption -ContinuationPromptForegroundColor $oldBackground
    Set-PSReadlineOption -Token None -ForegroundColor $oldBackground
    Set-PSReadlineOption -Token Command -ForegroundColor "Blue"
    Set-PSReadlineOption -Token Type -ForegroundColor "DarkGray"
    Set-PSReadlineOption -Token Number -ForegroundColor "DarkGreen"
    Set-PSReadlineOption -Token Member -ForegroundColor "DarkGreen"
    Set-PSReadlineOption -Token Variable -ForegroundColor "DarkGray"
    $Host.PrivateData.ErrorBackgroundColor = "Yellow"


Function reset-consolecolors {
    $host.ui.rawui.ForegroundColor = "DarkYellow"
    $host.ui.rawui.BackgroundColor = "DarkMagenta"

    Set-PSReadlineOption -ResetTokenColors

    $Host.PrivateData.ErrorForegroundColor = "Red"
    $Host.PrivateData.ErrorBackgroundColor = "Black"
    $Host.PrivateData.WarningForegroundColor = "Yellow"
    $Host.PrivateData.WarningBackgroundColor = "Black"
    $Host.PrivateData.DebugForegroundColor = "Yellow"
    $Host.PrivateData.DebugBackgroundColor = "Black"
    $Host.PrivateData.VerboseForegroundColor = "Yellow"
    $Host.PrivateData.VerboseBackgroundColor = "Black"
    $Host.PrivateData.ProgressForegroundColor = "Yellow"
    $Host.PrivateData.ProgressBackgroundColor = "DarkCyan"



As you can see there are multiple sets of colors you have to adjust.  The $host.ui.rawui object controls the overall background color of the console window.  The PSReadlineOption object controls the syntax highlighting colors.  Finally the $Host.PrivateData object controls the colors for errors and warnings. 

The invert-consolecolors function inverts the default color scheme swapping the background and foreground colors.  I additionally tweak a few of the syntax highlighting colors so they are legible in the lighter theme.


You can use the reset-consolecolors to reset the colors back to the defaults.



Not sure this is the best way to accomplish this but it serves my needs.  Hopefully it helps others.

Let me know in the comments below if you have a better way to accomplish this or have questions.

Leave a Reply