Reputation: 97
In Write-Host you can set the foreground color like
Write-Host "test" -ForegroundColor Green
Can you use hex codes? Like
Write-Host "test" -ForegroundColor FFFFFF
If I want the foreground color to be a color not listed in
[System.Enum]::GetValues([System.ConsoleColor])
What do I do?
Upvotes: 2
Views: 3605
Reputation: 27428
I suppose you can get into escape codes like psreadline. Run "get-psreadlineoptions" to see some of them. The docs for that command have a link to the codes. https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_ In powershell 7 you can use "`e" for escape.
write-host "$([char]0x1b)[91mhi"
hi # appears red
Command "$([char]0x1b)[31m" red # 93 bright yellow
Comment "$([char]0x1b)[32m" green
ContinuationPrompt "$([char]0x1b)[37m" white # 33 yellow
DefaultToken "$([char]0x1b)[37m" white
Emphasis "$([char]0x1b)[96m" bright cyan
Error "$([char]0x1b)[91m" bright red
Keyword "$([char]0x1b)[92m" bright green
Member "$([char]0x1b)[97m" bright white
Number "$([char]0x1b)[97m" bright white
Operator "$([char]0x1b)[90m" bright black
Parameter "$([char]0x1b)[90m" bright black
Selection "$([char]0x1b)[30;47m" black on white # 35;43 magenta;yellow
String "$([char]0x1b)[36m" cyan
Type "$([char]0x1b)[37m" white
Variable "$([char]0x1b)[92m" bright green
There's a module called Pansies that does it. It installs a new write-host. https://www.reddit.com/r/PowerShell/comments/hi0c0v/module_monday_pansies/ It supports xterm colors too, DodgerBlue, etc...
Upvotes: 2
Reputation: 61028
Another option could be to create a hashtable map of hex codes and console color names and use that:
$colorMap = [ordered]@{
'000000' = 'Black'
'140E88' = 'DarkBlue'
'00640A' = 'DarkGreen'
'008B87' = 'DarkCyan'
'8B0000' = 'DarkRed'
'820087' = 'DarkMagenta'
'AAAA00' = 'DarkYellow'
'A9A9A9' = 'Gray'
'808080' = 'DarkGray'
'0000FF' = 'Blue'
'00FF00' = 'Green'
'00FFFF' = 'Cyan'
'FF0000' = 'Red'
'FF00FF' = 'Magenta'
'FFFF00' = 'Yellow'
'FFFFFF' = 'White'
}
foreach($colorCode in $colorMap.Keys) {
Write-Host "Testing color $colorCode" -ForegroundColor $colorMap[$colorCode]
}
Of course, using this you are restricted to the hex codes the $colorMap
contains as key, otherwise an exception is thrown
Upvotes: 0