Set the encoding to ANSI in PowerShell 2.0

大城市里の小女人 提交于 2019-12-20 02:28:09

问题


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

回答1:


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"



回答2:


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.



来源:https://stackoverflow.com/questions/37700256/set-the-encoding-to-ansi-in-powershell-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!