-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Converting from windows 1252 to UTF8 #6550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
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 Regrettably, This is an oversight that must be corrected. My suggestion: add an The - cumbersome - workaround to use in the meantime requires use of the .NET framework directly:
Or, more generically:
|
PowerShell Core 6.0 accepts We can write as follow. $content = $_ | Get-Content -Encoding ([System.Text.Encoding]::GetEncoding(1252))
# or
$content = $_ | Get-Content -Encoding ([System.Text.Encoding]::GetEncoding([cultureinfo]::CurrentCulture.TextInfo.ANSICodePage)) Additionally, It is better to discuss this RFC if compatibility is necessary. Maybe #5204 related. |
Ah, thanks. Somehow I had wrongly convinced myself that you couldn't directly pass a 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 Again, given that |
Certainly, to introduce |
The workaround proposed by mklement0 works for me. |
@Calimerou: Alternatively, we could retitle your issue and modify the initial post to propose the missing |
I would prefer yours. |
For now, I work around this issue in my scripts as follows:
HTH |
Steps to reproduce
Using Windows 1252 encoding, create a file "test.txt" that contents this sentence :
cette fonction doit être appelée avant l'initialisation de l'API
Try to convert the file "test.txt" from Windows 1252 to UTF8 using this script.
Param (
[Parameter(Mandatory=$True)][String]$SourcePath
)
Get-ChildItem $SourcePath* -recurse -Include *.txt | ForEach-Object {
$content = $_ | Get-Content
Set-Content -PassThru $_.Fullname $content -Encoding UTF8 -Force}
Expected behavior
In UTF8 :
cette fonction doit être appelée avant l'initialisation de l'API
Actual behavior
In UTF8:
cette fonction doit �tre appel�e avant l'initialisation de l'API
Environment data
Name Value
PSVersion 6.1.0-preview.1
PSEdition Core
GitCommitId v6.1.0-preview.1
OS Microsoft Windows 6.1.7601 S
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Note
Powershell 4.0 does not have this issue
The text was updated successfully, but these errors were encountered: