Storing PowerShell ISE editor preferences using .NET isolated storage

by ravikanthchaganti on May 18, 2010
226 Views

This evening I wrote a post on how we can use .NET isolated storage in PowerShell. As a follow up, I thought it will be useful if I can show a use case for this. So, in this post, I will show a profile script I just wrote that saves any changes made to $psISE.Options and restores these options automatically using .NET isolated storage and .NET XML serialization/deserialization. This is really straightforward.

Here is the script

[Reflection.Assembly]::LoadWithPartialName("System.IO")
[Reflection.Assembly]::LoadWithPartialName("System.IO.IsolatedStorage")
[Reflection.Assembly]::LoadWithPartialName("System.Xml")
[Reflection.Assembly]::LoadWithPartialName("System.Xml.Serialization")            

function New-EventAction {
    param ([System.Management.Automation.PSEventArgs]$objEvent)
    $changedProperty = ($objEvent.SourceEventArgs.PropertyName).ToString()
    Write-Host "Value of $changedProperty changed to $($objEvent.SourceArgs.Get(0).$changedProperty)"            

    $useriStorage = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreForAssembly()
    $file = New-Object System.IO.IsolatedStorage.IsolatedStorageFileStream("UISettigs.xml",[System.IO.FileMode]::Create,$useriStorage)
    $xml = New-Object System.Xml.Serialization.XmlSerializer($PSISE.Options.GetType())
    $xml.Serialize($file,$psISE.Options)
    $file.Close()
    $useriStorage.Close()
}            

function Update-ISEOptions {
    $useriStorage = [System.IO.IsolatedStorage.IsolatedStorageFile]::GetUserStoreForAssembly()
    try {
        $file = New-Object System.IO.IsolatedStorage.IsolatedStorageFileStream("UISettigs.xml",[System.IO.FileMode]::Open,$useriStorage)
    }
    catch {
        Write-Host "UISettings.xml is not found"
        return
    }            

    $xmlReader = New-Object System.Xml.XmlTextReader($file)
    $xml = New-Object System.Xml.Serialization.XmlSerializer($PSISE.Options.GetType())
    $newISEOptions = $xml.Deserialize($xmlReader)
    $psISE.Options.SelectedScriptPaneState=$newISEOptions.SelectedScriptPaneState
    $psISE.Options.ShowToolBar=$newISEOptions.ShowToolBar
    $psISE.Options.FontSize=$newISEOptions.FontSize
    $psISE.Options.FontName=$newISEOptions.FontName
    $psISE.Options.ErrorForegroundColor=$newISEOptions.ErrorForegroundColor
    $psISE.Options.ErrorBackgroundColor=$newISEOptions.ErrorBackgroundColor
    $psISE.Options.WarningForegroundColor=$newISEOptions.WarningForegroundColor
    $psISE.Options.WarningBackgroundColor=$newISEOptions.WarningBackgroundColor
    $psISE.Options.VerboseForegroundColor=$newISEOptions.VerboseForegroundColor
    $psISE.Options.VerboseBackgroundColor=$newISEOptions.VerboseBackgroundColor
    $psISE.Options.DebugForegroundColor=$newISEOptions.DebugForegroundColor
    $psISE.Options.DebugBackgroundColor=$newISEOptions.DebugBackgroundColor
    $psISE.Options.OutputPaneBackgroundColor=$newISEOptions.OutputPaneBackgroundColor
    $psISE.Options.OutputPaneTextBackgroundColor=$newISEOptions.OutputPaneTextBackgroundColor
    $psISE.Options.OutputPaneForegroundColor=$newISEOptions.OutputPaneForegroundColor
    $psISE.Options.CommandPaneBackgroundColor=$newISEOptions.CommandPaneBackgroundColor
    $psISE.Options.ScriptPaneBackgroundColor=$newISEOptions.ScriptPaneBackgroundColor
    $psISE.Options.ScriptPaneForegroundColor=$newISEOptions.ScriptPaneForegroundColor
    $psISE.Options.ShowWarningForDuplicateFiles=$newISEOptions.ShowWarningForDuplicateFiles
    $psISE.Options.ShowWarningBeforeSavingOnRun=$newISEOptions.ShowWarningBeforeSavingOnRun
    $psISE.Options.UseLocalHelp=$newISEOptions.UseLocalHelp
    $psISE.Options.CommandPaneUp = $newISEOptions.CommandPaneUp            

    $file.Close()
    $useriStorage.close()
}            

if ($psise) {
    Update-ISEOptions
    Register-ObjectEvent -InputObject $psISE.Options -EventName PropertyChanged -Action { New-EventAction $Event } | Out-Null
}

You can just copy paste this in to your PowerShell profile script. I have put checks for $psise since I have a common profile for all the editors/consoles I am using.  After this — once you open PowerShell ISE and change any settings like background colors, font styles, etc, it all gets saved in to UISettings.xml automatically. So, next time when you open ISE, all those settings will still be intact. Using .NET XML serializer and deserializer makes it a snap to create/read XML.

This is just one example of how we can use .NET isolated storage in PowerShell. Hope this helps..!

Related Posts

{ 3 trackbacks }

Leave a Comment

Previous post:

Next post: