Private/Get-IslOemEncoding.ps1
|
function Get-IslOemEncoding { <# .SYNOPSIS The OEM code page encoding a console-less powershell.exe writes its output in. .DESCRIPTION Windows PowerShell's [Console]::OutputEncoding defaults to the system OEM code page (437 on US systems). That is why an em dash came back as a hyphen and a check mark as a square root sign in Intune's reports. Read it from the registry rather than CultureInfo, which lies under invariant globalization, and register the legacy code pages when running on .NET Core. .PARAMETER Kind OEM (the default) is what a console-less powershell.exe writes its output in; ANSI is what Windows PowerShell 5.1 reads a file without a BOM as. .EXAMPLE (Get-IslOemEncoding).CodePage 437 on a United States English device. .INPUTS None. .OUTPUTS System.Text.Encoding. #> [CmdletBinding()] [OutputType([System.Text.Encoding])] param( [ValidateSet('OEM', 'ANSI')] [string]$Kind = 'OEM' ) $valueName = if ($Kind -eq 'ANSI') { 'ACP' } else { 'OEMCP' } $codePage = if ($Kind -eq 'ANSI') { 1252 } else { 437 } try { $key = 'HKLM:\SYSTEM\CurrentControlSet\Control\Nls\CodePage' $value = (Get-ItemProperty -LiteralPath $key -Name $valueName -ErrorAction Stop).$valueName if ($value -match '^\d+$') { $codePage = [int]$value } } catch { Write-Verbose "$valueName not readable from the registry, assuming $codePage" } # Windows PowerShell has the code pages built in and no provider type to register if ('System.Text.CodePagesEncodingProvider' -as [type]) { try { [System.Text.Encoding]::RegisterProvider([System.Text.CodePagesEncodingProvider]::Instance) } catch { Write-Verbose "Code page provider not registered: $($_.Exception.Message)" } } try { [System.Text.Encoding]::GetEncoding($codePage) } catch { Write-Verbose "Code page $codePage unavailable; falling back to the console encoding" [Console]::OutputEncoding } } |