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
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
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"
Set-Content : Cannot process command because of one or more missing mandatory parameters: Value.
I only want to change encoding not the content. Is there a different command. –
Labyrinthine 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.
The question actually works. Note that ANSI is not ASCII, but is windows-1252 encoding https://en.wikipedia.org/wiki/Windows-1252 (see also iso-8859-1). It doesn't have a BOM. This is the powershell 5.1 default (with some exceptions like out-file or ">", which is utf16). Powershell 7 defaults to utf8 no bom.
echo á | set-content foo -Encoding Default
cat foo
á
echo á | set-content foo -Encoding ascii
cat foo
?
© 2022 - 2024 — McMap. All rights reserved.