public/Set/Set-ISETheme.ps1

function Set-ISETheme {
    <#
    .SYNOPSIS
        Applies a PowerShell ISE theme from a file, registry entry, or theme object.
 
    .DESCRIPTION
        The Set-ISETheme function activates an ISE theme by setting all related color and font options
        in the current PowerShell ISE session. The theme can be supplied as a file, a registered theme name
        from the registry, or an in-memory object (such as one returned from Get-ISETheme or Set-ISEColorAdjustment).
 
    .PARAMETER File
        The path to a `.ps1xml` file containing serialized ISE theme data.
 
    .PARAMETER ThemeName
        The name of an ISE theme stored in the registry.
 
    .PARAMETER ThemeObject
        A theme object (e.g., from Get-ISETheme or Set-ISEColorAdjustment) containing pre-parsed color values.
 
    .EXAMPLE
        Set-ISETheme -File "C:\Themes\Monokai.ps1xml"
 
        Loads and applies the Monokai theme from disk to the current ISE session.
 
    .EXAMPLE
        Set-ISETheme -ThemeName "SolarizedDark"
 
        Applies the registered SolarizedDark theme from the registry.
 
    .EXAMPLE
        $adjusted = Set-ISEColorAdjustment -ThemeData $theme -Darker
        Set-ISETheme -ThemeObject $adjusted
 
        Applies a darkened version of the currently active theme.
 
    .NOTES
        Author: Jeff Pollock
        GitHub: https://github.com/phriendx/ISEColorTheme.cmdlets
        Website: https://pxlabs.info
    #>


    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions','')]
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory, ParameterSetName='FromFile', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [string]$File,

        [Parameter(Mandatory, ParameterSetName='FromReg', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [string]$ThemeName,

        [Parameter(Mandatory, ParameterSetName='FromObj', ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
        [object]$ThemeObject
    )

    Begin {
        if ($null -ne $script:ISEThemeState.CurrentISETheme) {
            $script:ISEThemeState.CurrentISEThem = $null
        }
    }

    Process {
        # Retrieve theme data
        $xmlObjects = if ($ThemeObject) {
            $ThemeObject
        } elseif ($File) {
            Get-ISETheme -File $File
        } else {
            Get-ISETheme -ThemeName $ThemeName
        }

        # Set Base Colors
        $baseColorMap = @{
            ErrorForegroundColor            = 'ErrorForegroundColor'
            ErrorBackgroundColor            = 'ErrorBackgroundColor'
            WarningForegroundColor          = 'WarningForegroundColor'
            WarningBackgroundColor          = 'WarningBackgroundColor'
            VerboseForegroundColor          = 'VerboseForegroundColor'
            VerboseBackgroundColor          = 'VerboseBackgroundColor'
            DebugForegroundColor            = 'DebugForegroundColor'
            DebugBackgroundColor            = 'DebugBackgroundColor'
            ConsolePaneBackgroundColor      = 'ConsolePaneBackgroundColor'
            ConsolePaneTextBackgroundColor  = 'ConsolePaneTextBackgroundColor'
            ConsolePaneForegroundColor      = 'ConsolePaneForegroundColor'
            ScriptPaneBackgroundColor       = 'ScriptPaneBackgroundColor'
            ScriptPaneForegroundColor       = 'ScriptPaneForegroundColor'
        }

        foreach ($item in $xmlObjects) {
            if ($item.Class -eq 'Base' -and $baseColorMap.ContainsKey($item.Attribute)) {
                $psISE.Options.$($baseColorMap[$item.Attribute]) = $item.Hex
            }
        }

        # Set token colors and font
        foreach ($item in $xmlObjects) {
            switch ($item.Class) {
                'TokenColors'         { $psISE.Options.TokenColors[$item.Attribute] = $item.Hex }
                'ConsoleTokenColors'  { $psISE.Options.ConsoleTokenColors[$item.Attribute] = $item.Hex }
                { $_ -like 'XmlTokenColors*' } {
                    if ($item.Attribute.Length -gt 2) {
                        $psISE.Options.XmlTokenColors[$item.Attribute] = $item.Hex
                    }
                }
                'Other' {
                    $psISE.Options.FontName = $item.FontFamily
                    $psISE.Options.FontSize = $item.FontSize
                }
            }
        }
    }

    End {}
}