October 11th, 2012

PowerTip: Write PowerShell Output in Color Without Using Write-Host

Doctor Scripto
Scripter

Summary: Write colorized output to the Windows PowerShell console without using the Write-Host cmdlet.

Hey, Scripting Guy! Question How can you write output to the Windows PowerShell console without using the Write-Host cmdlet?

Hey, Scripting Guy! Answer Set ForegroundColor to a different color by using $host.Ui.RawUi, and then use the Write-Output cmdlet to write the output. When you are finished, set the ForegroundColor back to its original color.

PS C:\> $t = $host.ui.RawUI.ForegroundColor

PS C:\> $host.ui.RawUI.ForegroundColor = “DarkGreen”

PS C:\> Write-Output “this is green output”

this is green output

PS C:\> $host.ui.RawUI.ForegroundColor = $t

PS C:\>

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.