1
votes

I have a script that processes data from files and writes result based on a condition to txt. Given data are strings with words like: "Distribución" or "México". When processed, those special characters like "é" and "ó" are broken (typical white square or question mark).

How can i encode the output file to make it work with those characters? I tried encoding in Utf8, utf8 without BOM, it doesn't work. Here is to file writing line:

...| Out-file -encoding XXX .\result.txt

in XXX i tried ASCII, Utf8, nothing works :/

1
Which application you used to read the result file? Maybe its default font doesn't contain appropriate characters, so all you got are entity placeholders. Use a hex editor to see what's really in the result file to be sure.vonPryz
Import the file with UTF8 encoding , process and export also with UTF8 encoding.Adam Mnich
@AdamMnich i have a line like that to import: $FirstFile = Import-Csv -Delimiter ';' -Encoding UTF8 $FirstPath | Select-Object -ExpandProperty Url And it gives me "a parameter cannot be found that matches parameter 'encoding' ". FirstPath is path to a fileWolwgang
I meant to say import and export with the same encoding. Are the values displayed properly in Powershell? When you import back the file to Powershell are the special characters preserved?Adam Mnich
@AdamMnich Thanks, i was importing it in 1 encoding type and exporting in another...Wolwgang

1 Answers

2
votes

Out-File will always add a BOM. It's a particularly annoying "feature" of that Cmdlet. Unfortunately - to my knowledge - there is no quick way to save a file using UTF8 WITHOUT a BOM in powershell. You can, however, leverage .Net to do this. This isn't really production ready, but here's a quick example:

$outputPath = "D:\temp.txt"
$data = "Distribución or México"
[System.IO.File]::WriteAllLines($outputPath, $data)

Wrap it in a Cmdlet, function and / or module to make it reusable. Of course you can take more control over the file encoding with .Net too.