public/Export/Export-ISEThemeFile.ps1
function Export-ISEThemeFile { <# .SYNOPSIS Exports a single PowerShell ISE theme to a .ps1xml file. .DESCRIPTION The Export-ISEThemeFile function saves an ISE theme—either the currently applied one or a specified theme— to a .ps1xml file suitable for reimporting later. Themes are serialized in an XML format compatible with Import-ISEThemeFile. Optionally, the exported theme can be immediately registered with the ISE after saving. .PARAMETER Name The name to assign to the exported theme file (without extension). .PARAMETER Directory The destination folder where the theme file will be saved. .PARAMETER ISETheme (Optional) The name of a registered ISE theme to export. If omitted, the currently applied theme is exported. .PARAMETER SaveToISE If specified, imports the exported theme file into the ISE registry after saving. .EXAMPLE Export-ISEThemeFile -Name 'MyDarkTheme' -Directory 'C:\Themes' Exports the currently applied ISE theme to 'C:\Themes\MyDarkTheme.StorableColorTheme.ps1xml'. .EXAMPLE Export-ISEThemeFile -Name 'Solarized' -Directory 'C:\Themes' -ISETheme 'SolarizedDark' -SaveToISE Exports the 'SolarizedDark' theme and immediately saves it to the registry. .NOTES Author: Jeff Pollock GitHub: https://github.com/phriendx/ISEColorTheme.cmdlets Website: https://pxlabs.info #> [cmdletbinding()] param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [string]$Name, [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [string]$Directory, [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [string]$ISETheme, [Parameter(ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [switch]$SaveToISE ) process { # Determine whether exporting current theme or a saved theme if ($ISETheme) { $Theme = Get-ISETheme -ThemeName $ISETheme $XmlTheme = $Theme } else { $XmlTheme = Get-CurrentISETheme } # Build full file path $FilePath = Join-Path -Path $Directory -ChildPath "$Name.StorableColorTheme.ps1xml" # Create the XML writer with Unicode encoding $xmlWriter = New-Object System.Xml.XmlTextWriter($FilePath, [Text.Encoding]::Unicode) # Set formatting options $xmlWriter.Formatting = 'Indented' $xmlWriter.Indentation = 4 # Start the XML document $xmlWriter.WriteStartDocument() # Write root element with attributes $xmlWriter.WriteStartElement('StorableColorTheme') $xmlWriter.WriteAttributeString('xmlns:xsd', 'http://www.w3.org/2001/XMLSchema') $xmlWriter.WriteAttributeString('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance') # Write Keys element and content $xmlWriter.WriteStartElement('Keys') foreach ($Attribute in $XmlTheme) { switch ($Attribute.Class) { 'Base' { $xmlWriter.WriteElementString('string', $Attribute.Attribute) } 'TokenColors' { $xmlWriter.WriteElementString('string', "TokenColors\$($Attribute.Attribute)") } 'ConsoleTokenColors' { $xmlWriter.WriteElementString('string', "ConsoleTokenColors\$($Attribute.Attribute)") } 'XmlTokenColors' { $xmlWriter.WriteElementString('string', "XmlTokenColors\$($Attribute.Attribute)") } } } $xmlWriter.WriteEndElement() # Close Keys # Write Values element and content $xmlWriter.WriteStartElement('Values') foreach ($Attribute in $XmlTheme) { if ($Attribute.Class -in @('Base', 'TokenColors', 'ConsoleTokenColors', 'XmlTokenColors')) { $xmlWriter.WriteStartElement('Color') $xmlWriter.WriteElementString('A', $Attribute.A) $xmlWriter.WriteElementString('R', $Attribute.R) $xmlWriter.WriteElementString('G', $Attribute.G) $xmlWriter.WriteElementString('B', $Attribute.B) $xmlWriter.WriteEndElement() # Close Color } } $xmlWriter.WriteEndElement() # Close Values # Write Name and Font attributes from the 'Other' class object $OtherAttr = $XmlTheme | Where-Object { $_.Class -eq 'Other' } | Select-Object -Last 1 if ($OtherAttr) { $xmlWriter.WriteElementString('Name', $Name) $xmlWriter.WriteElementString('FontFamily', $OtherAttr.FontFamily) $xmlWriter.WriteElementString('FontSize', $OtherAttr.FontSize) } # Close root element $xmlWriter.WriteEndElement() # Close StorableColorTheme # End XML document $xmlWriter.WriteEndDocument() # Flush and close writer $xmlWriter.Flush() $xmlWriter.Close() # Optionally import theme into ISE if ($SaveToISE) { Import-ISEThemeFile $FilePath } } } |