11

In a Windows PowerShell script, I want to execute code which converts a pipeline to CSV format, and encoding should be in UTF-8. How can I do this?

Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
user3189976
  • 111
  • 1
  • 1
  • 4
  • 2
    Please accept the answer as this helps people out there to quickly find out what a working solution is. The first answer solved the same problem for me. – Marcel Dutt Nov 12 '18 at 16:19

2 Answers2

30

Add this to the end of your pipeline:

| Export-Csv -Encoding UTF8

It's that simple.

The -Encoding parameter is available for any cmdlet that outputs to a file--Out-File, Set-Content, Add-Content, Export-Clixml, probably some others I'm not thinking of. One gotcha to watch out for is that for UTF16, you need to specify Unicode instead of UTF16 (I think the latter makes more sense, and should at least be available as a synonym, but apparently Microsoft doesn't). The full list of options is:

Unicode,UTF7,UTF8,ASCII,UTF32,BigEndianUnicode,Default,OEM

A couple of notes:

  • ASCII technically doesn't give you ASCII encoding, it gives you Windows codepage 1252 (which is based on extended ASCII and often informally referred to as ASCII).
  • Default gives you whichever of the other options is the system default. You can find out what the default is with [System.Text.Encoding]::Default.
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
  • I have to use `| Export-Csv -Encoding UTF8 -NoTypeInformation -Path out.csv` ([about -notype](https://stackoverflow.com/a/47102147/1026)) – Nickolay Sep 05 '19 at 11:16
0
Invoke-Sqlcmd -ServerInstance localhost -Database "$database" -Query "$qry"  -username "$username" -password "$password" | convertto-CSV -notype | out-file -encoding UTF8 -filepath $filename

This works in Powershell 2, and in situations where convertto-CSV doesn't permit the -encoding parameter (whereas outfile does)

Martin Cleaver
  • 909
  • 8
  • 21
  • i'm unclear why you would need to use `converto-csv -notype | out-file encoding UTF-8 -filepath $path` instead of `export-csv -encoding UTF-8 -path $path`. Can you explain the difference and why you'd use the former over the latter? – Chris Rudd May 21 '19 at 19:56