Change Display Output Colors in PowerShell ISE


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 

Comments (3)

  1. Bryan says:

    Too bad the image doesn’t match what happens.

    One of the problems with $host is that it makes certain assumptions that aren’t real.

    I have my powershell.exe set shortcut set so that the background is bright cyan (color 11) on blue. $host thinks that it is 15, and that’s what it gets reset to. Evidently the shortcut setting isn’t communicated to the program, and just assumes the default.

    Maybe instead of depending on the shortcut to set the color of the screen, I should have a $host.ui.RawUI.ForegroundColor = 11 in my profile instead, which would then always set the raw ui to what is should be.

  2. ed wilson says:

    @Bryan it does match what happens … just after it was done running 🙂 To really see what happens would require a video to show the color changings. I think I will do this over the weekend.

  3. L. says:

    I find System.ConsoleColor a bit restrictive compared to what is possible in current terminal emulators on unix (which often have escape sequences for 256 colors nowadays), and there was really no technical reason for such a limitation in the ISE. Of course,
    I can guess what happened: this was tailored for the console host, since it’s easier to limit the ISE version than to try to do something reasonable for colors that cannot be displayed by a console. Still, "16 colors is enough for everyone" is a (minor) annoyance.

Skip to main content