How to set Powershell background color programmatically to RGB Value

╄→гoц情女王★ 提交于 2021-02-04 13:25:28

问题


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.


回答1:


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



回答2:


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])



回答3:


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)



来源:https://stackoverflow.com/questions/18685772/how-to-set-powershell-background-color-programmatically-to-rgb-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!