You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The default encoding in PowerShell Core is now UTF-8 (without a BOM when creating files).
That means that a Windows 1252-encoded file - in the absence of a BOM defining it as such (there is none for Windows 1252) - is now interpreted as UTF-8.
The upshot is that you must now tell Get-Content what encoding to assume - unless it is UTF-8 or there is a BOM.
Regrettably, Get-Content doesn't currently allow you to specify Windows 1252, because Default now represents UTF-8 and no longer the active "ANSI" code page (such as Windows 1252), as on Windows PowerShell, and you cannot pass a [System.Text.Encoding] instance directly.
This is an oversight that must be corrected.
My suggestion: add an ANSI encoding enumeration value on Windows that represents the system's legacy "ANSI" code page (e.g., Windows 1252 on US-English systems).
The - cumbersome - workaround to use in the meantime requires use of the .NET framework directly:
Ah, thanks. Somehow I had wrongly convinced myself that you couldn't directly pass a System.Text.Encoding instance - thanks for clarifying that.
I think the discussion around the linked RFC eventually led to the current Core behavior of globally defaulting to BOM-less UTF-8 - see PowerShell/PowerShell-RFC#71
The WindowsLegacy meta-setting was intended for a never-implemented $PSDefaultEncoding preference variable, and was meant to globally revert to the old, inconsistent encoding behavior for the sake of backward compatibility - an approach that I personally think is not worth pursuing.
Again, given that OEM - the OEM code page implied by the legacy system locale - already exists as a predefined encoding enumeration value, it should be complemented with an ANSI identifier for the "ANSI" code page implied by the system locale (on Windows only; the equivalent of what Default represents for Windows PowerShell).
The workaround proposed by mklement0 works for me.
I propose to close this issue since the rest of the discussion is mainly focused on BM-less UTF8 which is indeed treated in PowerShell/PowerShell-RFC#71.
Thanks.
Calimerou: Alternatively, we could retitle your issue and modify the initial post to propose the missing ANSI encoding-enumeration value, as discussed. If you prefer my creating a new issue instead, let me know.
Activity
mklement0 commentedon Apr 3, 2018
The default encoding in PowerShell Core is now UTF-8 (without a BOM when creating files).
That means that a Windows 1252-encoded file - in the absence of a BOM defining it as such (there is none for Windows 1252) - is now interpreted as UTF-8.
The upshot is that you must now tell
Get-Contentwhat encoding to assume - unless it is UTF-8 or there is a BOM.Regrettably,
Get-Contentdoesn't currently allow you to specify Windows 1252, becauseDefaultnow represents UTF-8 and no longer the active "ANSI" code page (such as Windows 1252), as on Windows PowerShell, and you cannot pass a[System.Text.Encoding]instance directly.This is an oversight that must be corrected.
My suggestion: add an
ANSIencoding enumeration value on Windows that represents the system's legacy "ANSI" code page (e.g., Windows 1252 on US-English systems).The - cumbersome - workaround to use in the meantime requires use of the .NET framework directly:
Or, more generically:
stknohg commentedon Apr 4, 2018
Michael Klement (@mklement0)
PowerShell Core 6.0 accepts
System.Text.Encodingclass in-Encodingparameter. (#5080)We can write as follow.
Additionally,
WindowsLegacygis proposed in RFC.(but
WindowsLegacygis not implemented yet...)It is better to discuss this RFC if compatibility is necessary.
Maybe #5204 related.
mklement0 commentedon Apr 4, 2018
Takuya Shibata (@stknohg):
Ah, thanks. Somehow I had wrongly convinced myself that you couldn't directly pass a
System.Text.Encodinginstance - thanks for clarifying that.I think the discussion around the linked RFC eventually led to the current Core behavior of globally defaulting to BOM-less UTF-8 - see PowerShell/PowerShell-RFC#71
The
WindowsLegacymeta-setting was intended for a never-implemented$PSDefaultEncodingpreference variable, and was meant to globally revert to the old, inconsistent encoding behavior for the sake of backward compatibility - an approach that I personally think is not worth pursuing.Again, given that
OEM- the OEM code page implied by the legacy system locale - already exists as a predefined encoding enumeration value, it should be complemented with anANSIidentifier for the "ANSI" code page implied by the system locale (on Windows only; the equivalent of whatDefaultrepresents for Windows PowerShell).stknohg commentedon Apr 4, 2018
Certainly, to introduce
ANSIis simpler and not globally as you say.I think it's good.
Calimerou commentedon Apr 4, 2018
The workaround proposed by mklement0 works for me.
I propose to close this issue since the rest of the discussion is mainly focused on BM-less UTF8 which is indeed treated in PowerShell/PowerShell-RFC#71.
Thanks.
mklement0 commentedon Apr 4, 2018
Calimerou: Alternatively, we could retitle your issue and modify the initial post to propose the missing
ANSIencoding-enumeration value, as discussed. If you prefer my creating a new issue instead, let me know.Calimerou commentedon Apr 4, 2018
I would prefer yours.
Thanks in advance.
sba923 commentedon Jul 18, 2019
For now, I work around this issue in my scripts as follows:
HTH