Change Colors Used by the Windows PowerShell ISE

Change Colors Used by the Windows PowerShell ISE

  • Comments 2
  • Likes

Summary: Learn how to change colors used by the Windows PowerShell ISE to customize your scripting environment.

 

Microsoft Scripting Guy Ed Wilson here. One of the things that is kind of lame about Charlotte is that there is not a good computer store. This is a rather surprising revelation considering the heavy concentration of high-tech workers in the area. It might be that one actually exists. However, after spending over an hour on the Internet and even spamming several of my fellow Microsoft workers, I struck out. It might be that I am picky. What I call a good computer store is a store the size of an American football stadium that has one really long aisle devoted to power supplies, another long aisle devoted to KVM switches (keyboard, video, mouse), and at least half of an aisle dedicated to CMOS batteries. Not to mention hard disk drives, cases, CPUs, motherboards, and all of the other fun things that go along with building a computer.

I happen to be a hardware geek from way back. One of the reasons I love Windows Management Instrumentation (WMI) is because of all the cool information I can pick up about hardware. I am planning some more hardware-related Hey, Scripting Guy! Blog posts in the next few weeks. For now, let me push a stack of stuff out of the way, and make room for my laptop so that I can spend some time playing with the Windows PowerShell ISE. Oh, no, a female humanoid with a camera. Dude, too late. She got me.

Photo of Ed "Hardware" Wilson

Last weekend, I discussed customizing the Windows PowerShell ISE, and I pointed out that the colors are all instances of the System.Windows.Media.Colors .NET Framework class.

While it is true that the MSDN documentation that covers the colors class includes a helpful chart of the colors, I am not always connected to the Internet, and therefore I need to be able to discover information on my own. When I am exploring, I like to use the Get-Member cmdlet. By passing the .NET Framework class inside a set of square brackets, static properties of a class can be revealed. This is shown here:

[windows.media.colors] | Get-Member -Static -MemberType property

Each color is returned as a static property from the colors class. The name of the static property is converted to a string and passed to the OutputPaneTextBackgroundColor property of the $psISE.options. This causes the background color that is found behind the text in the output pane to change to the color that is current in the loop. This is shown here:

$psISE.Options.OutputPaneTextBackgroundColor = ` 
([windows.media.colors]::$($_.name)).ToString()

The thing that took a bit of experimentation was the portion of the code that displays the name of the color, as well as the hexadecimal value of the color. The name of the color comes across the pipeline as the name property of the object. Next a `t is used to tab over one tab stop. When a specific color is referenced by static property name from the colors class, the makeup of the color is displayed as combination of ARGB. This is seen here.

PS C:\Users\ed.NWTRADERS> [windows.media.colors]::aqua 
ColorContext : 
A : 255 
R : 0 
G : 255 
B : 255 
ScA : 1 
ScR : 0 
ScG : 1 
ScB : 1

To use the AGRB values for a color, the hexadecimal version of the AGRB values is required. This can be returned by using a subexpression to force evaluation of the color property inside a string. This is easier done than said, as is seen here.

PS C:\Users\ed.NWTRADERS> "$([windows.media.colors]::aqua)"

#FF00FFFF

 

That is basically all there is to the Get-PSIseColorValues.ps1 script. The complete script is seen here.

Get-PsIseColorValues.ps1

[windows.media.colors] | Get-Member -Static -MemberType property | 
ForEach-Object { 
$psISE.Options.OutputPaneTextBackgroundColor = `
([windows.media.colors]::$($_.name)).tostring() 
"$($_.name) `t $([windows.media.colors]::$($_.name))" 
}

When the Get-PSIseColorValues.ps1 script runs, the output shown in the following image is displayed.

Image of output displayed when script runs

Join me tomorrow as I pull together the things we have looked at both today and last weekend into a single script that will create a custom menu in the Windows PowerShell ISE. It is way cool. We would love you to follow us on Twitter or Facebook. If you have any questions, send email to us at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

 

Ed Wilson and Craig Liebendorfer, Scripting Guys

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • $psISE.Options.OutputPaneTextBackgroundColor = `  

    should be:

    $psISE.Options.OutputPaneTextBackgroundColor =

    i.e. drop the `

  • @John, I added the line continuation character ` due to the need to break up the line of code for publishing to the blog. You are correct in that if you make a complete single line out of the code it will work. When the blog was published, an extra space was added behind the line continuation character, so that when you copy and paste from the blog an error is generated. I have edited the above post to remove the space after the backtick ` character, and therefore there is no need to remove the line continuation character. Therefore $psISE.Options.OutputPaneTextBackgroundColor = `

    ([windows.media.colors]::$($_.name)).tostring()

    is correct.