Set the encoding to ANSI in PowerShell 2.0

2019-02-25 13:17发布

I want to set the encoding of a file to ANSI using the parameter -Encoding of the Set-Content cmdlet, I tried this one but it didn't work:

Set-Content -LiteralPath "$filePath" -Encoding Default

2条回答
成全新的幸福
2楼-- · 2019-02-25 13:54

PowerShell v2 doesn't recognize an argument Default for the parameter -Encoding. Use the argument Ascii to save a file with ANSI encoding:

Set-Content -LiteralPath "$filePath" -Encoding Ascii

or omit the parameter entirely (Set-Content defaults to ANSI encoding):

Set-Content -LiteralPath "$filePath"
查看更多
Fickle 薄情
3楼-- · 2019-02-25 14:13

I use the .NET WriteAllText function for that:

[IO.File]::WriteAllText($filePath, (Get-Content $filePath))

Ensure the default encoding reflects your desired output using:

[System.Text.Encoding]::Default

Otherwise, add the enum with the desired encoding as the third parameter.

查看更多
登录 后发表回答