Private/Utility/Update-DollarSignProfile.ps1
|
function Update-DollarSignProfile { <# .SYNOPSIS Adds UTF-8 output encoding configuration to the PowerShell profile. .DESCRIPTION Updates the PowerShell profile ($PROFILE) to include UTF-8 console output encoding configuration. Adds the line '[Console]::OutputEncoding = [Text.Encoding]::GetEncoding(65001)' at the beginning of the profile if it doesn't already exist. This ensures that PowerShell console output properly displays international characters, emojis, and special Unicode symbols by setting the output encoding to UTF-8 (CodePage 65001). The function is idempotent - it checks if the UTF-8 encoding line already exists in the profile and only adds it if missing, preventing duplicate entries on repeated runs. .INPUTS None. This function does not accept pipeline input. .OUTPUTS None. This function does not produce output unless -Verbose is specified. .EXAMPLE Update-DollarSignProfile Adds UTF-8 output encoding to the PowerShell profile if not already present. .EXAMPLE Update-DollarSignProfile -Verbose Updates the profile and displays verbose output about what was done. Output: "UTF-8 output encoding added to $PROFILE" or "UTF-8 output encoding already configured." .NOTES This function modifies the user's PowerShell profile ($PROFILE). The UTF-8 encoding setting will take effect in new PowerShell sessions after running this function. To apply the setting to the current session, either: - Restart PowerShell, or - Run: . $PROFILE The profile file will be created if it doesn't exist. CodePage 65001 is the universal identifier for UTF-8 encoding. .LINK https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles .LINK https://learn.microsoft.com/en-us/dotnet/api/system.console.outputencoding #> [CmdletBinding()] param ( ) #requires -Version 5.1 try { # Read existing profile content (silently handle non-existent profile) $profileContent = Get-Content $PROFILE -Raw -ErrorAction SilentlyContinue $utf8EncodingLine = '[Console]::OutputEncoding = [Text.Encoding]::GetEncoding(65001)' Write-Verbose "Checking profile for UTF-8 encoding configuration: $PROFILE" # Check if UTF-8 encoding is already configured in the profile if ($profileContent -notlike "*OutputEncoding*GetEncoding*65001*") { # Add UTF-8 encoding line at the top of the profile $utf8EncodingLine + "`n" + $profileContent | Out-File -FilePath $PROFILE -Encoding UTF8 -Force -ErrorAction Stop Write-Verbose "UTF-8 output encoding added to $PROFILE" } else { Write-Verbose 'UTF-8 output encoding already configured in profile.' } } catch { $errorRecord = [System.Management.Automation.ErrorRecord]::new( $_.Exception, 'ProfileUpdateFailed', [System.Management.Automation.ErrorCategory]::WriteError, $PROFILE ) $PSCmdlet.WriteError($errorRecord) } } |