Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

You cannot use -Encoding option with Import-Csv in PowerShell 2.0

Context:

Problem:

In PowerShell 2.0, Import-Csv cmdlet doesn’t have -Encoding option.

Solution:

If you want to read a CSV file without CRLF in cells, you can use Get-Content -Encoding and ConvertFrom-Csv like:

PS > Get-Content -Encoding UTF8 ./foobar.csv | ConvertFrom-Csv

If you want to read a CSV file with CRLF in cells, you should convert the CSV file to UTF-16 with BOM, and read the file with Import-Csv (Import-Csv can recognize UTF-16 encoding automatically even in PowerShell 2.0):

PS > Get-Content -Encoding UTF8 ./foobar.csv | Out-File -Encoding Unicode -Path "./foobar-utf16.csv"
PS > Import-Csv ./foobar-utf16.csv