List of all colors available for PowerShell?

≯℡__Kan透↙ 提交于 2019-12-09 05:20:52

问题


I am searching for a list of all colors I can use in PowerShell. Since we need to provide names and no hexnumbers, it's hard to figure out if a color exists or not, at least if you don't know how :))

For example, as -foregroundcolor

write-host "hello world" -foregroundcolor "red"

回答1:


The console colors are in an enum called [System.ConsoleColor]. You can list all the values using the GetValues static method of [Enum]

[Enum]::GetValues([System.ConsoleColor])

or just

[Enum]::GetValues([ConsoleColor])



回答2:


Pretty grid

$colors = [enum]::GetValues([System.ConsoleColor])
Foreach ($bgcolor in $colors){
    Foreach ($fgcolor in $colors) { Write-Host "$fgcolor|"  -ForegroundColor $fgcolor -BackgroundColor $bgcolor -NoNewLine }
    Write-Host " on $bgcolor"
}

https://gist.github.com/timabell/cc9ca76964b59b2a54e91bda3665499e




回答3:


I've found it useful to preview how the console colors will display with a simple helper function:

function Show-Colors( ) {
  $colors = [Enum]::GetValues( [ConsoleColor] )
  $max = ($colors | foreach { "$_ ".Length } | Measure-Object -Maximum).Maximum
  foreach( $color in $colors ) {
    Write-Host (" {0,2} {1,$max} " -f [int]$color,$color) -NoNewline
    Write-Host "$color" -Foreground $color
  }
}



回答4:


How about checking the help? Like so, get-help write-host will tell you:

[-BackgroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}]
[-ForegroundColor {Black | DarkBlue | DarkGreen | DarkCyan | DarkRed | DarkMagenta | DarkYellow | Gray | DarkGray | Blue | Green | Cyan | Red | Magenta | Yellow | White}]



回答5:


Here is an examle of displaying all color combinations of background and foreground colors.

$FGcolors = [enum]::GetValues([System.ConsoleColor])
$BGcolors = [enum]::GetValues([System.ConsoleColor])

Foreach ($FGcolor in $FGcolors)
{
    Foreach ($BGcolor in $BGcolors)
    {
        Write-Host ("Foreground: $FGColor BackGround: $BGColor")  -ForegroundColor $FGcolor -BackgroundColor $BGcolor
    }
}


来源:https://stackoverflow.com/questions/20541456/list-of-all-colors-available-for-powershell

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