How to set Powershell background color programmatically to RGB

The current selection of 16 colors from console colors is not suitable for me. I would like to use much darker options for the background.

I could install them using the user interface and change the RGB value there.

Selecting Blue and changing value to 65

For example, I can select Darkblue and select 65 for Blue in the RGB section (128 by default). Can someone tell me how to do this programmatically.

Sort of:

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

But with additional options.

+8
source share
3 answers

This old post by Lee Holmes explains how you can change the color to whatever value you want. You must 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 
+8
source

This powershell function mimics a 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' 

The console color names can be obtained using the following code:

 [Enum]::GetValues([ConsoleColor]) 
+4
source

I added this function to my powershell profile, as there is a program that regularly changes 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 pointer type objects than the actual copy of the object. This means that if you later set another variable equal to "(Get-Host) .UI.RawUI" and change things, the value of $ DefaultColors will also change (which is why I have necessarily copied them here as strings).

I tried to set other colors (using hexadecimal codes) with very little luck, although I found Powershell color settings with hexadecimal values ​​in the profile of the script (I just have not tried it since I am not particularly fond of the registry error, and the list of colors by default looks enough).

I also found this document: https://technet.microsoft.com/en-us/library/ff406264.aspx , which may need to be used later to figure out how to change my "grep", (currently I have there is aliased for select-string)

+1
source

Source: https://habr.com/ru/post/953438/


All Articles