I'm using a Powershell script as follows to convert a string to XML then export to a file (done this way to keep indenting):
[xml]$xmloutput = $xml
$sw = New-Object System.IO.StringWriter
$writer = New-Object System.Xml.XmlTextWriter($sw)
$writer.Formatting = [System.Xml.Formatting]::Indented
$xmloutput.WriteContentTo($writer)
$sw.ToString() | Set-Content -Encoding 'ASCII' $filepath
The destination has to be ASCII formatted due to a vendor restriction. The issue I'm seeing is ASCII just changes special characters into questions marks (example: Ö becomes ?).
If I use UTF8 encoding the output looks totally fine. I've even tried saving to UTF8 then converting to ASCII, does the same thing (exports a question mark):
[System.Io.File]::ReadAllText($filepath) | Out-File -FilePath $filepath -Encoding ASCII
If I try and replace the characters in the string before the conversion to XML (using ASCII code Ö
) it simply converts the ampersand and leaves the rest, making it useless.
Is there any way to have Powershell correctly save those characters into the file?
EDIT: I would like to see the special character in the outputted file, but if that is not ASCII-compliant, I'd like to see the ASCII code for it (in this example, Ö
)
I also don't want to see just an O, I need the actual character.
Ö
– chazbot7-Encoding Default
or-Encoding OEM
. Per this Ms document. docs.microsoft.com/en-us/powershell/module/… – Squashman