How to set Powershell background color programmatically to RGB Value How to set Powershell background color programmatically to RGB Value powershell powershell

How to set Powershell background color programmatically to RGB Value


This old post by Lee Holmes explains how you can go about changing the color to any value you want. You have to change the registry - http://www.leeholmes.com/blog/2008/06/01/powershells-noble-blue/

Push-Location Set-Location HKCU:\Console New-Item ".\%SystemRoot%_system32_WindowsPowerShell_v1.0_powershell.exe" Set-Location ".\%SystemRoot%_system32_WindowsPowerShell_v1.0_powershell.exe"New-ItemProperty . ColorTable00 -type DWORD -value 0×00562401 New-ItemProperty . ColorTable07 -type DWORD -value 0x00f0edee New-ItemProperty . FaceName -type STRING -value "Lucida Console" New-ItemProperty . FontFamily -type DWORD -value 0×00000036 New-ItemProperty . FontSize -type DWORD -value 0x000c0000 New-ItemProperty . FontWeight -type DWORD -value 0×00000190 New-ItemProperty . HistoryNoDup -type DWORD -value 0×00000000 New-ItemProperty . QuickEdit -type DWORD -value 0×00000001 New-ItemProperty . ScreenBufferSize -type DWORD -value 0x0bb80078 New-ItemProperty . WindowSize -type DWORD -value 0×00320078 Pop-Location


This powershell function mimics the cmd line call: color b0

function Set-ConsoleColor ($bc, $fc) {    $Host.UI.RawUI.BackgroundColor = $bc    $Host.UI.RawUI.ForegroundColor = $fc    Clear-Host}Set-ConsoleColor 'cyan' 'black'

Console color names can be retrieved with the following code:

[Enum]::GetValues([ConsoleColor])


I've added this function to my powershell profile since there is a program that regularly messes up the colors of my shell.

$DefaultForeground = (Get-Host).UI.RawUI.ForegroundColor$DefaultBackground = (Get-Host).UI.RawUI.BackgroundColorfunction SetColors{    Param    (        [string]$Foreground = "",        [string]$Background = ""    )    $ValidColors = "black","blue","cyan","darkblue" ,"darkcyan","darkgray",        "darkgreen","darkmagenta","darkred","darkyellow","gray","green",        "magenta","red","white","yellow";    $Foreground = $Foreground.ToLower()    $Background = $Background.ToLower()    if ( $Foreground -eq "" )    {        $Foreground = $DefaultForeground    }    if ( $Background -eq "" )    {        $Background = $DefaultBackground    }    if ( $ValidColors -contains $Foreground -and         $ValidColors -contains $Background )    {        $a = (Get-Host).UI.RawUI        $a.ForegroundColor = $Foreground        $a.BackgroundColor = $Background    }    else     {        write-host "Foreground/Background Colors must be one of the following:"        $ValidColors     }}set-alias set-colors SetColors

Some notes:

"$DefaultCololrs = (Get-Host).UI.RawUI" creates more of a pointer-type object than an actual copy of the object. This means that if you later set a different variable equal to "(Get-Host).UI.RawUI", and change things, $DefaultColors will also change (which is why I've made sure to copy them here as strings).

I tried setting other colors (using hex codes) with very little luck, though I did find Setting Powershell colors with hex values in profile script (I just haven't tried it yet, since I'm not particularly fond of mucking about in the registry, and the default list of colors seemed rather sufficient).

I also found this document: https://technet.microsoft.com/en-us/library/ff406264.aspx, which I may have to use later to figure out how to modify my "grep" command (currently I have it aliased to select-string)