Gary's $this and that about PowerShell

I am a Microsoft Senior Premier Field Engineer based out of Atlanta, GA. My focus is Powershell. This blog is mainly to share interesting Powershell script samples.

Sample all PowerShell Console Colors

Sample all PowerShell Console Colors

  • Comments 6
  • Likes

I often need to pick console colors to use making host text a little easier to look at (coupled with smart indentation).  The –Foregroundcolor parameter of the Write-Host cmdlet is what I commonly use to set text colors.  I like to use lots of colors, but sometimes I forget what all the colors are and what they look like (the look is more important).  The Intellisense in the v3+ ISE is great that it suggests the color names (tab completion will as well), but sometimes I need to be reminded of what each looks like.  Here is a simple little one-liner that will show the color names in their appropriate color.  I use this line every so often to remind me.

PS > [enum]::GetValues([System.ConsoleColor]) | Foreach-Object {Write-Host $_ -ForegroundColor $_}      

Black        

DarkBlue      

DarkGreen      

DarkCyan      

DarkRed      

DarkMagenta      

DarkYellow      

Gray      

DarkGray      

Blue      

Green      

Cyan    

Red      

Magenta      

Yellow

White <-- White is here, but you can’t see it)

Hopefully this trick helps someone out.

-Gary Siepser

Comments
  • Thanks for this, just what I needed :)

  • DarkMagenta is actually more blue-ish. default background of the console.

  • Very nice and thanks. In the "give an inch and take a mile" method I would like to ask one more question. How would we take each of those colors and try each of them to show on each of the possible back ground colors? Basically creating a pallet so you can see what you want for best flare and more important for my old eyes best contrast for readability.

  • $colors = [enum]::GetValues([System.ConsoleColor])
    foreach( $fcolor in $colors )
    {
    foreach( $bcolor in $colors )
    {
    Write-Host "ForegroundColor is : $fcolor BackgroundColor is : $bcolor "-ForegroundColor $fcolor -BackgroundColor $bcolor
    }
    }

  • Change your UI


    #Create variable with all colors

    $colors = [enum]::GetValues([System.ConsoleColor]) #| Foreach-Object {Write-Host $_ -ForegroundColor $_}

    #$colors | Foreach-Object {Write-Host $_ -ForegroundColor $_}



    #get two random numbres

    $randomint1 = Get-Random -Minimum 0 -Maximum $colors.Length

    $randomint2 = Get-Random -Minimum 0 -Maximum $colors.Length

    #$randomint1

    #$randomint2



    #set two random color varibles

    $color1 = $colors.GetValue($randomint1)

    $color2 = $colors.GetValue($randomint2)

    #$color1

    #$color2



    #set host UI colors

    $Host.ui.RawUI.BackgroundColor = $color1

    $Host.UI.RawUI.ForegroundColor = $color2



    #Set whole window

    cls



  • Random UI Color
    Updated


    #Get System Colors
    $colors = [enum]::GetValues([System.ConsoleColor])

    $colors



    #Create while variables
    $int1 = 0

    $int2 = 0



    )#DARK LIGHT/LIGH DARK loop
    while($int1 -eq 0

    {

    )#Random Int loop
    while($int2 -eq 0

    {
    #Get two int. compare for match
    $randomint1 = Get-Random -Minimum 0 -Maximum ($colors.Length-1)

    $randomint2 = Get-Random -Minimum 0 -Maximum ($colors.Length-1)

    if($randomint1 -ne $randomint2)

    {

    $int2 = 1
    }

    }#end random int loop

    $randomint1

    $randomint2



    #Get colors from $colors variable using two random int
    $color1 = $colors.GetValue($randomint1)

    $color2 = $colors.GetValue($randomint2)

    if($color1 -match "dark" -and $color2 -notmatch "dark" -and $color2 -notmatch "black")

    {

    Write-Host "DARK LIGHT MATCH"

    $int1 = 1

    }

    elseif($color1 -notmatch "dark" -and $color2 -match "dark" -and $color1 -notmatch "black")

    {

    Write-Host "LIGHT DARK MATCH"

    $int1 = 1

    }

    else

    {

    Write-Host "BOTH/NO MATCH"

    $int2 = 0

    }


    }#end DARK LIGHT/LIGHT DARK loop

    $color1

    $color2



    #Set Host UI fore and background colors
    $Host.UI.RawUI.BackgroundColor = $color1

    $Host.UI.RawUI.ForegroundColor = $color2



    #Set whole screen to current host UI colors
    cls

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment