如何以编程方式将Powershell背景色设置为RGB值
当前从控制台颜色中选择的16种颜色对我来说不是正确的选择。我想用这些颜色更深的变体作为背景 我肯定可以使用UI设置这些,并在那里更改RGB值 例如,我可以在RGB部分中选择暗蓝色并选择65作为蓝色(默认值为128)。有人能告诉我如何通过编程来实现这一点吗 比如:如何以编程方式将Powershell背景色设置为RGB值,powershell,console,Powershell,Console,当前从控制台颜色中选择的16种颜色对我来说不是正确的选择。我想用这些颜色更深的变体作为背景 我肯定可以使用UI设置这些,并在那里更改RGB值 例如,我可以在RGB部分中选择暗蓝色并选择65作为蓝色(默认值为128)。有人能告诉我如何通过编程来实现这一点吗 比如: (Get-Host).UI.RawUI.BackgroundColor=DarkBlue 但是还有其他选择。这篇由Lee Holmes撰写的旧文章解释了如何将颜色更改为您想要的任何值。您必须更改注册表- 此powershell函数模
(Get-Host).UI.RawUI.BackgroundColor=DarkBlue
但是还有其他选择。这篇由Lee Holmes撰写的旧文章解释了如何将颜色更改为您想要的任何值。您必须更改注册表-
此powershell函数模拟cmd行调用:
color b0
function Set-ConsoleColor ($bc, $fc) {
$Host.UI.RawUI.BackgroundColor = $bc
$Host.UI.RawUI.ForegroundColor = $fc
Clear-Host
}
Set-ConsoleColor 'cyan' 'black'
可以使用以下代码检索控制台颜色名称:
[Enum]::GetValues([ConsoleColor])
我已将此功能添加到我的powershell配置文件中,因为有一个程序会经常弄乱我的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
一些注意事项:
“$DefaultCololrs=(Get Host).UI.RawUI”创建的指针类型对象多于对象的实际副本。这意味着,如果稍后将另一个变量设置为“(Get Host.UI.RawUI”)并进行更改,$DefaultColors也将更改(这就是为什么我确保将它们作为字符串复制到这里)
我尝试设置其他颜色(使用十六进制代码),但运气很差,不过我确实找到了(我只是还没有尝试过,因为我不太喜欢在注册表中乱搞,默认的颜色列表似乎已经足够了)
我还发现了这个文档:,我以后可能不得不使用它来确定如何修改我的“grep”命令(目前我将它化名为select string)在输入命令之前,它可能会重复。然后又回到旧的配色方案。
$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