Summary: Microsoft MVP Ravikanth shows how to automatically save and restore Windows PowerShell ISE preference settings with an easy-to-use function.

 

Microsoft Scripting Guy Ed Wilson here. It is a fact that no script is ever completed. There is always something else that can be accomplished in the code. I have several goals as a Scripting Guy. The first is to illustrate things that can be done; next I want to provide ideas for further exploration; last, I want to create excitement in using Windows PowerShell to solve real-world problems. It is really cool when I see building on things I have illustrated. Here is a case in point.

 

Ravikanth is a Windows PowerShell MVP and works at Dell, Inc., as a lead engineer in the Microsoft SharePoint solutions team. He loves automation and is a Windows PowerShell fanatic. He writes regularly on his blog about topics related to Windows PowerShell, SharePoint, and Microsoft server virtualization. He is also a moderator on the Hey, Scripting Guy! Forum and a regular speaker at Bangalore IT Pro user group meetings.

Saving Windows PowerShell ISE preferences

Ed Wilson, the Microsoft Scripting Guy, has been doing a great job during the Weekend Scripter series of articles explaining how to customize the Windows PowerShell ISE. We can personalize the look and feel of this script editor. All this is possible because of the ISE object model and the wealth of information stored in $psISE.options. All this is great, but how do we make these changes applied every time you open ISE? There are two ways to achieve that.

The first method is to hard code all the changes we want in our Windows PowerShell ISE profile script. This is easy, but next time we want to change something, we need to edit the profile script and restart the Windows PowerShell ISE. This is tedious!

It would be great if there were a method to save our preferences as soon as we change something in $psISE.Options. In addition, it would be cool to have these changes applied every time we reopen the editor.

Yes, this second method of storing ISE color/font preferences is doable by using the Windows PowerShell ISE events. Windows PowerShell 2.0 introduced several cmdlets that enable us to access events from various objects. The $psISE.Options object supports events, too. To understand what events are supported by the $psISE.Options object, we can use the Get-Member cmdlet.

$psISE.Options | Get-Member -MemberType Event

   TypeName: Microsoft.PowerShell.Host.ISE.ISEOptions

Name                      MemberType     Definition

----                          ----------             ----------

PropertyChanged    Event                System.ComponentModel.PropertyChangedEventHandler...

Here, we see an event named PropertyChanged. This event is triggered every time a $psISE.Options property is modified. Let us see how we can subscribe to this event using the Register-ObjectEvent cmdlet.

Register-ObjectEvent -InputObject $psISE.Options -EventName PropertyChanged -Action { Write-Host ($Event.SourceEventArgs.PropertyName).ToString() }

Using the Register-ObjectEvent cmdlet, we create an event subscriber to listen to the events triggered by $psISE.Options object. In the –action script block, $Event.SourceEventArgs.PropertyName will contain the name of the property that changed. To verify that the event subscription is working, we will now change an ISE option. For example:

$psISE.Options.ScriptPaneBackgroundColor = "Green"

The moment we change the script pane background color, we should see this property name displayed in the ISE output pane.

So, within the Register-ObjectEvent cmdlet’s –action script block, we can do something more useful than just display the property name. We can use the action parameter to save the changed properties to an XML file so that we can restore the saved properties next time we open the Windows PowerShell ISE. We can achieve this using .NET XML serialization which makes it easy to convert an object into XML representation and vice versa.

Register-ObjectEvent -InputObject $psISE.Options -EventName PropertyChanged -Action {

$file = "C:\scripts\ISEPrefs.xml"

$xmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($PSISE.Options.GetType())

$xmlWriter = [System.Xml.XmlTextWriter]::Create($file)

$xmlSerializer.Serialize($xmlWriter,$psISE.Options)

$xmlWriter.Close()

}

If you see the above –action script block, we store the XML file path in $file and use .NET XML serialization to serialize the $psISE.Options object and store it in the ISEPrefs.xml file. When you have this new event subscription, make changes to any of the ISE options and you will see that ISEPrefs.xml is created at the location specified, and it has the object contents in XML representation.

That is great! But how can we deserialize that XML file into an object representation? We use .NET XML serialization again:

$file = "C:\scripts\ISEPrefs.xml"

$xmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($PSISE.Options.GetType())

$xmlReader = New-Object System.Xml.XmlTextReader($file)

$newISEOptions = $xmlSerializer.Deserialize($xmlReader)

Now, $newISEOptions has the stored ISE preferences from the XML file. We can just assign these values to the live $psISE.Options object properties. For example:

$psISE.Options.CommandPaneBackgroundColor = $newISEOptions.CommandPaneBackgroundColor

This will reset the commandpane background color to the value stored in the XML file. We can apply the same technique to restore the remaining stored properties also. We can also have this as a part of the Windows PowerShell ISE profile script to make sure our ISE preferences are stored and restored every time we use ISE.

I have put together a script that you can just simply paste in to your existing ISE profile. You can find this script on the TechNet Script Gallery.

 

Thank you, Ravikanth, for an excellent article.

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

 

Ed Wilson and Craig Liebendorfer, Scripting Guys