0

I'm trying to output a bunch of data and have some of the german Umlauts: ä, ö, ü,.. I was adding -Encoding UTF8 at the end of Export-Csv, but it does not really work. My code:

For ($i = 0; $i -lt $accFileCont.Length; $i++) {
        $accFileCont[$i] | Export-Csv $outAccFile -NoTypeInformation -Encoding UTF8
    }

I'm expecting ä, ö, ü... instead of "ä",... What is wrong here? It does not output the Umlauts. Thanks in advance :)

DoeJane
  • 1
  • 1
  • 1
  • 3
    Where does `$accFileCont` come from? If you're reading the strings from a file and fail to specify the correct encoding then, you're not going to be able to fix that afterwards – Mathias R. Jessen Apr 25 '19 at 16:28
  • 1
    And when you read the output, you should be using UTF-8. If there is a chance that you're not, please show the bytes. It would also be helpful to show the byte in the input file. – Tom Blodget Apr 25 '19 at 16:36
  • If the file doesn't have a BOM in the beginning, PS 5 won't display it correctly. Also, it looks like you're overwriting the csv instead of appending to it. Or you can move export-csv to outside the loop. If you append to it, the file needs to be UTF8 with BOM in the first place. – js2010 Apr 25 '19 at 19:49
  • Thanks for your answers! I am appending: `$accFileCont[$i] | Export-Csv $outAccFile -NoTypeInformation -Encoding UTF8` – DoeJane Apr 26 '19 at 07:52
  • How should I change the input file to be UTF8 with BOM? My input file looks like this: `Datum;Beschreibung;Beschreibung2;Wert 26.01.2019;Lastschrift Max Moritz IBAN: DE02100100100xxxxxxxxx BIC: PBNKDEFFXXX Gehalt UCI: DExxxxxxxxxxxxxx UMR: xxxxxxxxxxxx;Absender: Max Moritz, IBAN: DE02100100100xxxxxxxxx BIC: PBNKDEFFXXX;-200,00 19.01.2019;Lastschrift Hallo Pizza IBAN: DE02100100100xxxxxxxxx BIC: VRBUDE51 Beleg 123455667 UCI: DExxxxxxxxxxxxxx UMR: xxxxxxxxxxxx;Absender: Hallo Pizza IBAN: DE02100100100xxxxxxxxx BIC: VRBUDE51;-12,00` – DoeJane Apr 26 '19 at 07:52
  • Is it possible to set the input file to be UTF8 when I read it by using `Get-Content`? – DoeJane Apr 26 '19 at 07:54
  • 1
    Problem solved!!! Mathias, I found a comment from you to a question from last year :) I should have used `Import-Csv`, which **defaults to UTF-8**! :D `Export-Csv` **defaults to ASCII**.. Thanks guys ;) – DoeJane Apr 26 '19 at 08:36
  • Hmm, I don't get notified when others comment. import-csv will recognize whatever the encoding of the file is (except ansi). You're right, export-csv defaults to ascii, not ansi as the docs say. And to append a csv do `export-csv -append`. get-content and set-content use ansi encoding to work with accents a little better. You can check the current encoding of a file in notepad, using save as. Notepad can recognize utf8 and unicode without bom's or encoding signatures. – js2010 Apr 26 '19 at 13:25

1 Answers1

2

For me, it was actually the "-Encoding utf8BOM" which did the job

Environment: Windows 10 20H2 German, German Excel Version, PowerShell 7

My German Excel is not showing the "umlauts" correct, if I am not using utf8BOM.

There is a whole discussion about UTF8 with or without BOM

Actually "Marius" wrote the following comment: "It might not be recommended but it did wonders to my powershell script when trying to output "æøå""

The "C:\sources\new_18.json" is formatted in UTF-8 (it's a simple JSON File)

The "-Delimiter ";"" is for German Excel

    $TestVariable = (Get-Content -raw -Encoding UTF8 "C:\sources\new_18.json") | ConvertFrom-Json

    $TestVariable.value | Export-Csv "C:\sources\PSOutput\Output_$((Get-Date).ToString('MM-dd-yyyy_hh-mm-ss')).csv" -NoTypeInformation -Delimiter ";" -Encoding utf8BOM

For PowerShell 5: Change "-Encoding utf8BOM" to "-Encoding utf8" (In Powershell 5 "Export-Csv -Encoding utf8" saves documents in utf8BOM")