Private/Utility/Update-OutputEncoding.ps1
|
function Update-OutputEncoding { <# .SYNOPSIS Sets the console output encoding to UTF-8 for the current session. .DESCRIPTION Updates the console output encoding to UTF-8 (CodePage 65001) for the current PowerShell session. This ensures proper display of international characters, emojis, and special Unicode symbols in console output. This function only affects the current session. The encoding change is temporary and will not persist to new PowerShell sessions. For persistent UTF-8 encoding, use Update-DollarSignProfile to add the configuration to your PowerShell profile. .INPUTS None. This function does not accept pipeline input. .OUTPUTS None. This function does not produce output unless -Verbose is specified. .EXAMPLE Update-OutputEncoding Sets the console output encoding to UTF-8 for the current session. .EXAMPLE Update-OutputEncoding -Verbose Sets UTF-8 encoding and displays verbose confirmation. Output: "Console output encoding set to UTF-8 (CodePage 65001)" .EXAMPLE if (-not (Test-IsUtf8)) { Update-OutputEncoding } Conditionally sets UTF-8 encoding only if not already configured. .NOTES This function sets only the OUTPUT encoding, not input encoding. The change applies only to the current PowerShell session. For permanent configuration across all sessions, add this to your $PROFILE: [Console]::OutputEncoding = [Text.Encoding]::GetEncoding(65001) Or use Update-DollarSignProfile to automate profile configuration. CodePage 65001 is the universal identifier for UTF-8 encoding. .LINK https://learn.microsoft.com/en-us/dotnet/api/system.console.outputencoding .LINK https://learn.microsoft.com/en-us/windows/console/console-code-pages #> [CmdletBinding()] [OutputType([void])] param ( ) #requires -Version 5.1 try { # Set console output encoding to UTF-8 (CodePage 65001) [Console]::OutputEncoding = [Text.Encoding]::GetEncoding(65001) Write-Verbose "Console output encoding set to UTF-8 (CodePage 65001)" } catch { $errorRecord = [System.Management.Automation.ErrorRecord]::new( $_.Exception, 'EncodingUpdateFailed', [System.Management.Automation.ErrorCategory]::WriteError, 65001 ) $PSCmdlet.WriteError($errorRecord) } } |