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

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:\>


Originally Written by: The Scripting Guys at http://blogs.technet.com/b/heyscriptingguy/archive/2012/10/11/powertip-write-powershell-output-in-color-without-using-write-host.aspx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s