PowerTip: Use PowerShell to Create Color Output from Commands

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" }

Comments (4)

Cancel reply

  1. jrv says:

    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?)


    Have a great weekend guys and gals.

  2. jrv says:

    @Everybody –

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


    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.

  3. Walid Toumi says:


    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!')


  4. Walid Toumi says:


    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 🙂


    —>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])}


Skip to main content