August 25th, 2012

PowerTip: Use PowerShell to Create Color Output from Commands

Doctor Scripto
Scripter

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

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.