Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Powershell background color programmatically to RGB Value

The current selection of 16 colors from Console Colors is not the right choice for me. I would like to use much darker variants of these for the background.

I could definitely set these using the UI and changing the RGB value there.

Selecting Blue and changing value to 65

For example, I could choose Darkblue and select 65 for Blue in the RGB section(128 is the default). Could some one tell me how to do this programmatically.

Something like:

(Get-Host).UI.RawUI.BackgroundColor=DarkBlue

But with additional options.

like image 952
pradeep Avatar asked Sep 08 '13 16:09

pradeep


People also ask

How do I change the background color in PowerShell script?

To change the background color of the font, you can use the GUI and command line both. With GUI − Colors → Screen Background. You will notice that the background color of the text has been changed to DarkBlue.

Can you change the Colour of PowerShell?

Console colorsYou can change the general background colors through the $host. ui. rawui object and the colors for errors and warnings through the $Host. PrivateData object.

How do I change the default color in PowerShell?

Copy the ones you want to reset to their default color settings, and paste them in your "C:\Users\User\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Windows PowerShell" folder. That's it!


2 Answers

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.BackgroundColor
function 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)

like image 126
Garret Hoffman Avatar answered Sep 19 '22 16:09

Garret Hoffman


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
like image 25
manojlds Avatar answered Sep 20 '22 16:09

manojlds