PowerTip: Use PowerShell to Create Color Output from Commands

PowerTip: Use PowerShell to Create Color Output from Commands

  • Comments 4
  • Likes

Summary: Learn to use the Write-Host PowerShell cmdlet to create color output from commands.

Hey, Scripting Guy! Question Someone told me the Write-Host cmdlet could create color output. Can you give me some samples of acceptable syntax?

Hey, Scripting Guy! Answer

a. Write-Host -ForegroundColor 12 "hi"

b. Write-Host -ForegroundColor 12 "hi" -BackgroundColor white

c. Write-Host -ForegroundColor blue -BackgroundColor white

d. Write-Host -ForegroundColor 2 hi

e. Write-Host -backgroundcolor 2 hi

f. Write-Host -backgroundcolor ("{0:X}" -f 2) hi

     for($i=0 ; $i -le 15 ; $i++) { write-host -foregroundcolor $i "hi" }

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

    you can also use $Host.UI.Write or $Host.UI.Writeline:

    1..10 | % {

    $fg=[enum]::GetValues([ConsoleColor]) | Where { $_ -ne $([console]::BackgroundColor)}| Random

    $Host.UI.Writeline($fg,[console]::BackgroundColor,'Scripting Guy!')

    }

  • @Everybody -

    I told myself I was going to be good but --- antagonism is antagonism.

    Sampler:

    1..([int]::MaxValue) | % {

      $fg=[enum]::GetValues([ConsoleColor]) |

           Where{$_ -ne $([console]::BackgroundColor)}| Random

      $bg=[enum]::GetValues([ConsoleColor]) | Random

      $Host.UI.Write($fg,$bg,'Scripting Guy!')

    }

    Thanks Ed, Walid.

  • Some old Ed mischief along the same lines.

    What would it look like if we combiined these two nifty idioms. - (or do I mean idiots?)  Don't you just love langauge? (What was it that George Carlin was saying when he was so rudely interrupted?)

    blogs.technet.com/.../draw-boxes-and-lines-in-the-windows-powershell-console-host.aspx

    Have a great weekend guys and gals.

  • @jrv

    1..([int]::MaxValue) | % {

     $fg=[enum]::GetValues([ConsoleColor]) |

          Where{$_ -ne $([console]::BackgroundColor)}| Random

     $bg=[enum]::GetValues([ConsoleColor]) | Random

     $Host.UI.Write($fg,$bg,'Scripting Guy!')

    }

    ---> beautiful :)

    @Everybody

    --->here another, but to listen:

    Function Get-Jedi {

     $s = "262_500","393_500","350_100","330_100","294_100","525_500"

     $s = $s + "393_500","350_100","330_100","294_100","525_500","393_500","350_200"

     $s = $s + "330_200","350_200","294_400"

     $s |% {[console]::beep($_.split("_")[0],$_.split("_")[1])}

    }