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)
source share