{"id":16931,"date":"2010-10-03T00:01:00","date_gmt":"2010-10-03T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/10\/03\/weekend-scripter-automatically-save-and-restore-powershell-ise-preference-settings\/"},"modified":"2010-10-03T00:01:00","modified_gmt":"2010-10-03T00:01:00","slug":"weekend-scripter-automatically-save-and-restore-powershell-ise-preference-settings","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-automatically-save-and-restore-powershell-ise-preference-settings\/","title":{"rendered":"Weekend Scripter: Automatically Save and Restore PowerShell ISE Preference Settings"},"content":{"rendered":"<p>&nbsp;\n<b>Summary<\/b>: Microsoft MVP Ravikanth shows how to automatically save and restore Windows PowerShell ISE preference settings with an easy-to-use function.\n&nbsp;\nMicrosoft 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.\n&nbsp;\nRavikanth 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! <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en-US\/ITCG\/threads\">Forum<\/a> and a regular speaker at <a href=\"http:\/\/bitpro.in\/\">Bangalore IT Pro<\/a> user group meetings.<\/p>\n<h5>Saving Windows PowerShell ISE preferences<\/h5>\n<p>Ed Wilson, the Microsoft Scripting Guy, has been doing a great job during the Weekend Scripter <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc512613.aspx\">series of articles<\/a> explaining how to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/windows+powershell+ise\/\">customize the Windows PowerShell ISE<\/a>. 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 <b>$psISE.options<\/b>. All this is great, but how do we make these changes applied every time you open ISE? There are two ways to achieve that.\nThe 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!\nIt would be great if there were a method to save our preferences as soon as we change something in <b>$psISE.Options<\/b>. In addition, it would be cool to have these changes applied every time we reopen the editor.\nYes, this second method of storing ISE color\/font preferences is doable by using the Windows PowerShell ISE events. <a href=\"http:\/\/support.microsoft.com\/kb\/968929\">Windows PowerShell 2.0<\/a> introduced several cmdlets that enable us to access events from various objects. The <b>$psISE.Options<\/b> object supports events, too. To understand what events are supported by the <b>$psISE.Options<\/b> object, we can use the <b>Get-Member<\/b> cmdlet.<\/p>\n<blockquote><p>   $psISE.Options | Get-Member -MemberType Event<\/p>\n<p>&nbsp;&nbsp; TypeName: Microsoft.PowerShell.Host.ISE.ISEOptions<\/p>\n<p>Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; MemberType&nbsp;&nbsp;&nbsp;&nbsp; Definition<\/p>\n<p>&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-<\/p>\n<p>PropertyChanged&nbsp;&nbsp;&nbsp; Event&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; System.ComponentModel.PropertyChangedEventHandler&#8230;<\/p>\n<\/blockquote>\n<p>Here, we see an event named <b>PropertyChanged<\/b>. This event is triggered every time a <b>$psISE.Options<\/b> property is modified. Let us see how we can subscribe to this event using the <a href=\"http:\/\/www.bing.com\/visualsearch?g=powershell_cmdlets&amp;qpvt=Windows+PowerShell&amp;FORM=Z9GE36#category=2&amp;r=4\">Register-ObjectEvent<\/a> cmdlet.<\/p>\n<blockquote><p>   Register-ObjectEvent -InputObject $psISE.Options -EventName PropertyChanged -Action { Write-Host ($Event.SourceEventArgs.PropertyName).ToString() }<\/p>\n<\/blockquote>\n<p>Using the <b>Register-ObjectEvent<\/b> cmdlet, we create an event subscriber to listen to the events triggered by <b>$psISE.Options<\/b> object. In the <b>&ndash;action<\/b> script block, <b>$Event.SourceEventArgs.PropertyName<\/b> 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:<\/p>\n<blockquote><p>   $psISE.Options.ScriptPaneBackgroundColor = &#8220;Green&#8221;<\/p>\n<\/blockquote>\n<p>The moment we change the script pane background color, we should see this property name displayed in the ISE output pane.\nSo, within the <b>Register-ObjectEvent<\/b> cmdlet&rsquo;s <b>&ndash;action<\/b> script block, we can do something more useful than just display the property name. We can use the <b>action<\/b> 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.<\/p>\n<blockquote><p>   Register-ObjectEvent -InputObject $psISE.Options -EventName PropertyChanged -Action {<\/p>\n<p>$file = &#8220;C:scriptsISEPrefs.xml&#8221;<\/p>\n<p>$xmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($PSISE.Options.GetType())<\/p>\n<p>$xmlWriter = [System.Xml.XmlTextWriter]::Create($file)<\/p>\n<p>$xmlSerializer.Serialize($xmlWriter,$psISE.Options)<\/p>\n<p>$xmlWriter.Close()<\/p>\n<p>}<\/p>\n<\/blockquote>\n<p>If you see the above <b>&ndash;action<\/b> script block, we store the XML file path in <b>$file<\/b> and use .NET XML serialization to serialize the <b>$psISE.Options<\/b> 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.\nThat is great! But how can we deserialize that XML file into an object representation? We use .NET XML serialization again:<\/p>\n<blockquote><p>   $file = &#8220;C:scriptsISEPrefs.xml&#8221;<\/p>\n<p>$xmlSerializer = New-Object System.Xml.Serialization.XmlSerializer($PSISE.Options.GetType())<\/p>\n<p>$xmlReader = New-Object System.Xml.XmlTextReader($file)<\/p>\n<p>$newISEOptions = $xmlSerializer.Deserialize($xmlReader)<\/p>\n<\/blockquote>\n<p>Now, <b>$newISEOptions<\/b> has the stored ISE preferences from the XML file. We can just assign these values to the live <b>$psISE.Options<\/b> object properties. For example:<\/p>\n<blockquote><p>   $psISE.Options.CommandPaneBackgroundColor = $newISEOptions.CommandPaneBackgroundColor<\/p>\n<\/blockquote>\n<p>This will reset the <b>commandpane<\/b> 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.\nI have put together a script that you can just simply paste in to your existing ISE profile. You can find this script on the <a href=\"http:\/\/gallery.technet.microsoft.com\/ScriptCenter\/en-us\/30e3c667-578c-4929-bee2-1d316f7aa579\">TechNet Script Gallery.<\/a>\n&nbsp;\nThank you, Ravikanth, for an excellent article.\nWe invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n&nbsp;<\/p>\n<p><b>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Microsoft MVP Ravikanth shows how to automatically save and restore Windows PowerShell ISE preference settings with an easy-to-use function. &nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[56,82,3,4,61,45,100],"class_list":["post-16931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-guest-blogger","tag-ravikanth","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell","tag-windows-powershell-ise"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Microsoft MVP Ravikanth shows how to automatically save and restore Windows PowerShell ISE preference settings with an easy-to-use function. &nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=16931"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/16931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=16931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=16931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=16931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}