1
votes

When used with .Net Framework 2.0 it looks like the OEM encoding isn't available in PowerShell.

PS1> Get-Content $FilePath -Encoding OEM

Cannot bind parameter 'Encoding'. Cannot convert value "OEM" to type "Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding" due to invalid enumeration values. Specify one of the following enumeration values and try again. The possible enumeration values are "Unknown, String, Unicode, Byte, BigEndianUnicode, UTF8, UTF7, Ascii".

However I have files in this encoding that I'd like to read in a string, how to do it?

1
"OEM" is not an encoding, but a subset of all encodings. With one assigned to each locale. Therefore changing locale will change its effects. The subset of encodings PowerShell's FileSystem encoder supports is defined by the FileSystemCmdletProviderEncoding enumeration. I note this includes Oem: maybe case is important (or PowerShell version).Richard
Thanks; couldn't find anything clear in MSDN about it. Is it really the same thing as the "current culture oem code page"?CharlesB
I assume so, without disassembling the filesystem provider (significantly non-trivial) one can only test... Also note that the set of encodings listed by get-help get-content -param encoding is smaller than in the enumeration.Richard
It's implied by the question, but to make it explicit: PowerShell v3 now DOES support the Oem encoding value directly; the - excellent - workaround below is only needed in v2.mklement0

1 Answers

4
votes

Read in a binary array and decode it :

$enc = [System.Text.Encoding]::GetEncoding($Host.CurrentCulture.TextInfo.OEMCodePage)
$bytes = [System.IO.File]::ReadAllBytes($FilePath)
$text = $enc.GetString($bytes)