public/Import/Import-ISEThemeFile.ps1

function Import-ISEThemeFile {
    <#
    .SYNOPSIS
        Imports an ISE theme XML file into the registry.
 
    .DESCRIPTION
        The Import-ISEThemeFile function reads a `.ps1xml` theme file and registers it in the PowerShell ISE theme
        registry key. The function can also optionally apply the imported theme to the current session after import.
 
    .PARAMETER FilePath
        The path to a `.ps1xml` file or a FileInfo object representing an ISE theme to import. Accepts pipeline input.
 
    .PARAMETER ApplyTheme
        If specified, applies the imported theme to the current PowerShell ISE session.
 
    .EXAMPLE
        Import-ISEThemeFile "C:\ISEThemes\MyTheme.ps1xml" -ApplyTheme
 
        Imports the specified ISE theme file into the registry and applies it to the current session.
 
    .EXAMPLE
        Get-ChildItem *.ps1xml | Import-ISEThemeFile
 
        Pipes a list of theme files into the function.
 
    .NOTES
        Author: Jeff Pollock
        GitHub: https://github.com/phriendx/ISEColorTheme.cmdlets
        Website: https://pxlabs.info
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
        [object]$FilePath,

        [Parameter()]
        [switch]$ApplyTheme
    )

    process {
        # Normalize FileName if passed as FileInfo object
        if ($FilePath -is [System.IO.FileInfo]) {
            $FilePath = $FilePath.FullName
        }

        if (-not (Test-Path $FilePath)) {
            Write-Warning "File not found: $FilePath"
            return
        }

        try {
            $Rawxml = Get-Content -Path $FilePath -Raw
            [xml]$ThemeData = $RawXml

            $ThemeName = $ThemeData.StorableColorTheme.Name

            # Write theme to registry
            $RegPath = "HKEY_CURRENT_USER\Software\Microsoft\PowerShell\3\Hosts\PowerShellISE\ColorThemes"

            if (Get-ItemProperty -Path registry::$RegPath -Name $ThemeName) {
                Set-ItemProperty -Path registry::$RegPath -Name $ThemeName -Value $Rawxml
            } else {
                New-ItemProperty -Path registry::$RegPath -Name $ThemeName -Value $Rawxml -Type String -Force
            }            

            if ($ApplyTheme) {
                Set-ISETheme -ThemeName $ThemeName
            }

        } catch {
            Write-Error "Failed to import theme file '$FilePath': $_"
        }
    }
}