Powershell: Change encoding for Import-Csv

Import-Csv does not accept ANSI files.
Also semicolon delimiter needs to be specified.

$personsFilename = ".\persons.csv"
#Change encoding from ANSI to UTF-8 by reading and saving
#-Encoding:String # = ANSI = Windows-1252 = iso-8859-1

#print content on screeen
Get-Content $personsFilename -Encoding:String | ft

#convert to UTF-8. type is alias for Get-Content
type $personsFilename -Encoding:String | Out-File $personsFilename"UTF8.csv" -Encoding:UTF8

#Import-Csv by default accepts UTF-8
Import-Csv $personsFilename"UTF8.csv" -Delimiter:";" | Where-Object {$_.UserID -ieq "ABC"} | ft
Import-Csv $personsFilename"UTF8.csv" -Delimiter:";" | Where-Object {$_.UserID -ieq "ABC"} | Export-Csv $personsFilename"FilteredUTF8.csv" -NoTypeInformation -Delimiter:";" -Encoding:UTF8
$personsFile = Import-Csv $personsFilename"UTF8.csv" -Delimiter:";"

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: