Set the encoding to ANSI in PowerShell 2.0

Zakaria Belghiti picture Zakaria Belghiti · Jun 8, 2016 · Viewed 25.8k times · Source

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

Answer

Ansgar Wiechers picture Ansgar Wiechers · Jun 8, 2016

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"