November 9th, 2015

Change Display Output Colors in PowerShell ISE

Doctor Scripto
Scripter

Summary: Ed Wilson, Microsoft Scripting Guy, talks about changing the output console colors in the Windows PowerShell ISE.

Microsoft Scripting Guy, Ed Wilson, is here. One of the things I like about using the Write-Host cmdlet is that I can change the color of a line that writes to the Windows PowerShell console. But, what if I want to change the entire output console so that the output is really obvious? I can also do that on the fly by using the $host object. As shown here, the $host object returns a number of properties, and it contains a number of other objects:

PS C:\Users\mredw> $host

Name             : Windows PowerShell ISE Host

Version          : 5.0.10240.16384

InstanceId       : 92ba5361-53ee-4217-82d2-bf54710efcbe

UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface

CurrentCulture   : en-US

CurrentUICulture : en-US

PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions

DebuggerEnabled  : True

IsRunspacePushed : False

Runspace         : System.Management.Automation.Runspaces.LocalRunspace

The embedded object I want is the host interface object. It is in the UI property. When I access it, I see that there is a RawUI property that contains another object:

PS C:\Users\mredw> $host.UI

RawUI                                                                 

—–                                                                  

System.Management.Automation.Internal.Host.InternalHostRawUserInterface 

The RawUI object is really cool and it contains a number of interesting properties:

PS C:\Users\mredw> $host.UI.RawUI

ForegroundColor       : White

BackgroundColor       : -1

CursorPosition        : 0,0

WindowPosition        :

CursorSize            :

BufferSize            : 85,0

WindowSize            :

MaxWindowSize         :

MaxPhysicalWindowSize :

KeyAvailable          :

WindowTitle           : Windows PowerShell ISE

If I want to know what the possible enumeration values are for ForegroundColor, I can give it a bogus number and look at the returned error message:

Image of Message

It tells me that the enumeration is System.ConsoleColor, and that the possible values are: Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Gray, DarkGray, Blue, Green, Cyan, Red, Magenta, Yellow, and White.

If I count them, that is 16 possible values, and I bet I can go from 0 to 15.

Armed with this information, I decide to write a little script that will change the foreground color to a different value every second. I create an array of numbers from 0 to 15 by using the range operator:

0..15.

Then I read the current foreground color and store it in a variable:

$color = $Host.UI.RawUI.ForegroundColor

Now I use the Foreach-Object cmdlet, pipe the numbers across the pipeline, and assign new ForegroundColor values:

Foreach-Object {

$Host.UI.RawUI.ForegroundColor = $_

I print a message that says what the foreground color is, and then I sleep for a second. I continue to loop through the numbers. When I am done, I revert back to the original color. The complete script is shown here:

Clear-Host

$color = $Host.UI.RawUI.ForegroundColor

0..15 |

Foreach-Object {

$Host.UI.RawUI.ForegroundColor = $_

"The console color is now $_"

Start-Sleep 1}

"Now setting it back to default …"

$Host.UI.RawUI.ForegroundColor = $color 

When I run it, the following output appears:

Image of Command Output

That is all there is to using Windows PowerShell to change the ISE output console colors.  Join me tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Category
Scripting

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.