Exploring the Windows PowerShell ISE Color Objects

Exploring the Windows PowerShell ISE Color Objects

  • Comments 1
  • Likes

Summary: Join Microsoft Scripting Guy Ed Wilson as he takes you on a guided tour of the Windows PowerShell ISE color objects.


Microsoft Scripting Guy Ed Wilson here. It is a beautiful sunny afternoon. I have decided to embrace the sunshine and accept the humidity that seems to be irrevocably joined with it. I have Deep Purple cranked up on my Zune HD, a great soundtrack for playing around with Windows PowerShell. I have a glass of Pelegrino (a habit I picked up during my many trips to Italy) and a fresh Lamington I picked up at an Australian Bakery I found on the Internet. I got addicted to Lamingtons from the first bite. I was working in Brisbane, Australia, teaching a VBScript workshop I had developed from the Microsoft Press VBScript Self-Paced Learning Edition book I wrote. My bride was joining me for the second week, so I was waiting at the Brisbane International Airport when I happened to look at the status board for the flight from Los Angeles. Because it was delayed for a couple of hours, I headed over to the little café they have and innocently bought a Lamington and a cup of English Breakfast tea. That was all it took. From then on, my life has been one continual search for the perfect Lamington and a quiet place in which to write scripts. Deep Purple seems to be just the right type of quiet and it pairs perfectly with a fresh Lamington.

Anyway, I have finally found time to address a project I have wanted to examine for some time (having been restricted from my woodworking shop due to a foot injury and consequently growing tired of reading Shakespeare). The project I want to undertake today is to look at modifying the Windows PowerShell ISE.

Inside the Windows PowerShell ISE, an automatic variable $psISE is created to represent the ISE. You can use Windows PowerShell cmdlets such as Format-List and Get-Member to explore the objects that are returned by the $psISE variable. This is shown here:

PS C:\Users\edwils> $psISE | Format-List *
CurrentPowerShellTab : Microsoft.PowerShell.Host.ISE.PowerShellTab
CurrentFile : Microsoft.PowerShell.Host.ISE.ISEFile
Options : Microsoft.PowerShell.Host.ISE.ISEOptions
PowerShellTabs : {PowerShell 1}

Each of the properties exposed by $psISE returns an object, and that means they are all ripe for exploration. In fact, the $psISE itself is an object, as revealed by the Get-Member cmdlet output shown here:

PS C:\Users\edwils> $psISE | Get-Member

   TypeName: Microsoft.PowerShell.Host.ISE.ObjectModelRoot

Name                 MemberType    Definition
----                 ----------   ----------
Equals               Method        bool Equals(System.Object obj)
GetHashCode          Method        int GetHashCode()
GetType              Method        type GetType()
ToString             Method        string ToString()
CurrentFile          Property      Microsoft.PowerShell.Host.ISE.ISEFile CurrentFile {get;}
CurrentPowerShellTab Property      Microsoft.PowerShell.Host.ISE.PowerShellTab CurrentPowerShellTab {get;}   
Options              Property      Microsoft.PowerShell.Host.ISE.ISEOptions Options {get;}
PowerShellTabs       Property      Microsoft.PowerShell.Host.ISE.PowerShellTabCollection PowerShellTabs {get;}


To look at the options available for the Windows PowerShell ISE, choose the Options property. The result is shown here: 

PS C:\Users\edwils> $psISE.Options
SelectedScriptPaneState : Top
ShowToolBar : True
TokenColors : {[Attribute, #FFADD8E6], [Command, #FF0000FF], [CommandArgument, #FF8A2BE2]
, [CommandParameter, #FF000080]...}
DefaultOptions : Microsoft.PowerShell.Host.ISE.ISEOptions
FontSize : 20
FontName : Lucida Console
ErrorForegroundColor : #FFFF0000
ErrorBackgroundColor : #00FFFFFF
WarningForegroundColor : #FFFF8C00
WarningBackgroundColor : #00FFFFFF
VerboseForegroundColor : #FF0000FF
VerboseBackgroundColor : #00FFFFFF
DebugForegroundColor : #FF0000FF
DebugBackgroundColor : #00FFFFFF
OutputPaneBackgroundColor : #FFF0F8FF
OutputPaneTextBackgroundColor : #FFF0F8FF
OutputPaneForegroundColor : #FF000000
CommandPaneBackgroundColor : #FFFFFFFF
ScriptPaneBackgroundColor : #FFFFFFFF
ScriptPaneForegroundColor : #FF000000
ShowWarningForDuplicateFiles : True
ShowWarningBeforeSavingOnRun : False
UseLocalHelp : True
CommandPaneUp : False

To get an idea of what is returned via the Options property, pipe the returned object to the Get-Member cmdlet. This reveals that a Microsoft.PowerShell.Host.ISE.ISEOptions object is returned. The members of the ISEOptions object are shown here:

PS C:\Users\edwils> $psISE.Options | gm

   TypeName: Microsoft.PowerShell.Host.ISE.ISEOptions

Name                       MemberType    Definition
----                       ----------    ----------
PropertyChanged            Event         System.ComponentModel.PropertyChangedEventHandler PropertyChang
Equals                     Method        bool Equals(System.Object obj)
GetHashCode                Method        int GetHashCode()
GetType                    Method        type GetType()
RestoreDefaults            Method        System.Void RestoreDefaults()
RestoreDefaultTokenColors  Method        System.Void RestoreDefaultTokenColors()
ToString                   Method        string ToString()
CommandPaneBackgroundColor Property      System.Windows.Media.Color CommandPaneBackgroundColor {get;set;} 
CommandPaneUp              Property      System.Boolean CommandPaneUp {get;set;}
DebugBackgroundColor       Property      System.Windows.Media.Color DebugBackgroundColor {get;set;}       
DebugForegroundColor       Property      System.Windows.Media.Color DebugForegroundColor {get;set;}       
DefaultOptions             Property      Microsoft.PowerShell.Host.ISE.ISEOptions DefaultOptions {get;}   
ErrorBackgroundColor       Property      System.Windows.Media.Color ErrorBackgroundColor {get;set;}       
ErrorForegroundColor       Property      System.Windows.Media.Color ErrorForegroundColor {get;set;}        
FontName                   Property      System.String FontName {get;set;}
FontSize                   Property      System.Int32 FontSize {get;set;}
OutputPaneBackgroundColor  Property      System.Windows.Media.Color OutputPaneBackgroundColor {get;set;}  
OutputPaneForegroundColor  Property      System.Windows.Media.Color OutputPaneForegroundColor {get;set;}  
OutputPaneTextBackgroundColor Property   System.Windows.Media.Color OutputPaneTextBackgroundColor {get;s...
ScriptPaneBackgroundColor  Property      System.Windows.Media.Color ScriptPaneBackgroundColor {get;set;}  
ScriptPaneForegroundColor  Property      System.Windows.Media.Color ScriptPaneForegroundColor {get;set;}  
SelectedScriptPaneState    Property      Microsoft.PowerShell.Host.ISE.SelectedScriptPaneState SelectedS...
ShowToolBar                Property      System.Boolean ShowToolBar {get;set;}
ShowWarningBeforeSavingOnRun Property    System.Boolean ShowWarningBeforeSavingOnRun {get;set;}           
ShowWarningForDuplicateFiles Property    System.Boolean ShowWarningForDuplicateFiles {get;set;}           
TokenColors                Property      System.Collections.Generic.IDictionary`2[[System.Management.Aut...
UseLocalHelp               Property      System.Boolean UseLocalHelp {get;set;}
VerboseBackgroundColor     Property      System.Windows.Media.Color VerboseBackgroundColor {get;set;}     
VerboseForegroundColor     Property      System.Windows.Media.Color VerboseForegroundColor {get;set;}     
WarningBackgroundColor     Property      System.Windows.Media.Color WarningBackgroundColor {get;set;}     
WarningForegroundColor     Property      System.Windows.Media.Color WarningForegroundColor {get;set;}

The TokenColors property returns a dictionary object that contains the color mappings for the Windows PowerShell ISE tokenizer. This is used to provide the coloration for the scrip pane. Here are the color mappings:

PS C:\Users\edwils> $psISE.Options.TokenColors

                        Key Value
                        --- -----
                  Attribute #FFADD8E6
                    Command #FF0000FF
            CommandArgument #FF8A2BE2
           CommandParameter #FF000080
                    Comment #FF006400
                   GroupEnd #FF000000
                 GroupStart #FF000000
                    Keyword #FF00008B
           LineContinuation #FF000000
                  LoopLabel #FF00008B
                     Member #FF000000
                    NewLine #FF000000
                     Number #FF800080
                   Operator #FFA9A9A9
                   Position #FF000000
         StatementSeparator #FF000000
                     String #FF8B0000
                       Type #FF008080
                    Unknown #FF000000
                   Variable #FFFF4500

The colors are all instances of the System.Windows.Media.Color .NET Framework class. The reason for using these colors is that it presents a richer palette than the 16 colors that are expressed in the standard command prompt. Upon discovering this information, I thought “cool”; unfortunately, the colors are only moderately cool because they are not settable. I discovered this information by examining only the TokenColors member as seen here. You will notice that it says that TokenColors is get—and therefore not set (this is the last thing displayed.)

PS C:\Users\edwils> $psISE.Options | gm tokencolors | ft -Wrap

   TypeName: Microsoft.PowerShell.Host.ISE.ISEOptions

Name       MemberType Definition                                                                         
----       ---------- ----------                                                                         
TokenColors Property  System.Collections.Generic.IDictionary`2[[System.Management.Automation.PSTokenType,
                       System.Management.Automation, Version=, Culture=neutral, PublicKeyToken=31bf3
                       856ad364e35],[System.Windows.Media.Color, PresentationCore, Version=, Culture
                       =neutral, PublicKeyToken=31bf3856ad364e35]] TokenColors {get;}

But in looking over the methods available from the Options, I saw the RestoreDefaultTokenColors method. Hmm, I thought, why is there a RestoreDefaultTokenColors method if there is no way to modify the token colors? Therefore, I decided to play around with the dictionary that is returned by the TokenColors property.

The first thing I did was index into the collection of token colors. This is shown here:

PS C:\Users\edwils> $psISE.Options.TokenColors[0]
ColorContext : 
A : 255
R : 0
G : 0
B : 0
ScA : 1
ScR : 0
ScG : 0
ScB : 0

After I did that, the next thing I did was pipe the color object that was returned into the Get-Member cmdlet to see if it is read/write. This is shown here:

PS C:\Users\edwils> $psISE.Options.TokenColors[0] | gm

   TypeName: System.Windows.Media.Color

Name                       MemberType    Definition
----                       ----------    ----------
Clamp                      Method        System.Void Clamp()
Equals                     Method        bool Equals(System.Windows.Media.Color color), bool Equals(System.Object o)
GetHashCode                Method        int GetHashCode()
GetNativeColorValues       Method        float[] GetNativeColorValues()
GetType                    Method        type GetType()
ToString                   Method        string ToString(), string ToString(System.IFormatProvider provider)       
A                          Property      System.Byte A {get;set;}
B                          Property      System.Byte B {get;set;}
ColorContext               Property      System.Windows.Media.ColorContext ColorContext {get;}
G                          Property      System.Byte G {get;set;}
R                          Property      System.Byte R {get;set;}
ScA                        Property      System.Single ScA {get;set;}
ScB                        Property      System.Single ScB {get;set;}
ScG                        Property      System.Single ScG {get;set;}
ScR                        Property      System.Single ScR {get;set;}

I then attempted to change one of the color values by assigning a value directly to one of the properties. Of course, I have no idea which TokenColor I am changing, but I figure if things get too bad off, I will close and re-open the Windows PowerShell ISE. If that does not work, I will attempt the RestoreDefaultTokenColors method and hope it works. Unfortunately, recovery was a nonissue because the assignment did not work. This is shown here:

PS C:\Users\edwils> $psISE.Options.TokenColors[0]
ColorContext : 
A : 255
R : 0
G : 0
B : 0
ScA : 1
ScR : 0
ScG : 0
ScB : 0
PS C:\Users\edwils> $psISE.Options.TokenColors[0].R = 255
PS C:\Users\edwils> $psISE.Options.TokenColors[0]
ColorContext : 
A : 255
R : 0
G : 0
B : 0
ScA : 1
ScR : 0
ScG : 0
ScB : 0

Next, I decided to attempt to use the item method method to retrieve the settings for a specific token color. As shown here, this works:

PS C:\Users\edwils> $psISE.Options.TokenColors.item("member") 
ColorContext : 
A : 255 
R : 0 
G : 0 
B : 0 
ScA : 1 
ScR : 0 
ScG : 0 
ScB : 0

Now, I decide to attempt something different in the way of value assignment. Remember the original chart that was listed with all the token colors in response to $psISE.Options.TokenColors? Each color was a single string. Therefore, I decided to change the member by using the command shown here:


When I attempted to up-arrow to retrieve the members, I could see immediately that the command had worked. The results are shown in the following image.

Image showing the command worked

You do not have to make up colors to use with TokenColors. In fact, there is a whole raft of predefined colors that have been defined in the System.Windows.Media.Colors .NET Framework class. The MSDN documentation includes a really cool chart that displays the color names and the ARGB values. For example, AliceBlue is #FFF0F8FF.

Join me tomorrow as I put this newfound information to use by creating a custom color profile. We would love you to follow us on Twitter and 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
  • made a web based color picker for ISE


    take a look and please provide feedback. if there is interest i'll put more effort in to it!