Need help importing a CSV file using IMPORT-CSV commandlet

Hello:

I am fairly new to Windows Powershell and need some help. I want to import a CSV file using the import-csv commandlet but getting some name errors because of the csv file header not containing quotation marks. I could manually add new header names in using the -header option but I want to script this.

I thought about importing the first line of the CSV file to some sort of array and then doing a replace to add the quotation marks in to each row name. Would get-content achieve this?

An example would be this:

name, address, phone number, school

converted to

"name", "address", "phone number", "school"

I'd want to be able to import the entire CSV file, cut the first row and do the change. Then pipe those results to import-csv commandlet. Any help would be much appreciated.

I am downloading this CSV file from a website that does not put the quotation marks on the header row.

July 10th, 2013 1:43am


By far the easiest means is to indeed use the -header parameter in Import-Csv with the help of the single and double-quotes as follows:

Import-Csv .\my.csv -Header '"name"', '"address"', '"phone"', '"number"', '"school"'

Please share with us if this helps. Thanks.


Free Windows Admin Tool Kit Click here and download it now
July 10th, 2013 2:51am

HI:

That would work, but I am trying to do this in a more automated way. The names of the columns are already in the CSV file. I just need to add quotation marks to the top Headers to make it in the right format to pipe into import-csv. The reason I want to automate this is that if those header names change it will not break the script and save a lot of manual revising because I have to go in and rename the headers using the -header option.

July 10th, 2013 1:38pm

Buddy, I'm not sure if it's a powershell V3 thing but I can't replicate your problem.  If I import a csv with no quotes on the first line the shell still assumes that the first line is the header.  

# csv content
a,b,c
1,1,1
2,1,1
1,2,2

Import-csv test.csv

a                                       b                                       c
-                                       -                                       -
1                                       1                                       1
2                                       1                                       1
1                                       2                                       2

Free Windows Admin Tool Kit Click here and download it now
July 10th, 2013 2:33pm

I guess my question is are you sure you need to do what you're trying to do?  From my experience import-csv just assumes the first line is the header.
July 10th, 2013 2:35pm

Buddy, I'm not sure if it's a powershell V3 thing but I can't replicate your problem.  If I import a csv with no quotes on the first line the shell still assumes that the first line is the header.  

Free Windows Admin Tool Kit Click here and download it now
July 10th, 2013 2:41pm

AFAIK, quotes are only needed if the field itself contains a comma. See these examples:

test1.csv:

a,b,c
1,1,1
2,1,1
1,2,2

test2.csv

d,"e, f",g
3,3,3
4,1,1
5,7,8

PS C:\Scripts\PowerShell Scripts\Misc Testing\7-10-13> ls


    Directory: C:\Scripts\PowerShell Scripts\Misc Testing\7-10-13


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/10/2013   2:44 PM         28 test1.csv
-a---         7/10/2013   2:44 PM         33 test2.csv


PS C:\Scripts\PowerShell Scripts\Misc Testing\7-10-13> Import-Csv .\test1.csv

a                                       b                                       c
-                                       -                                       -
1                                       1                                       1
2                                       1                                       1
1                                       2                                       2


PS C:\Scripts\PowerShell Scripts\Misc Testing\7-10-13> Import-Csv .\test2.csv

d                                       e, f                                    g
-                                       ----                                    -
3                                       3                                       3
4                                       1                                       1
5                                       7                                       8

July 10th, 2013 2:48pm

I was thinking something like this would work, how could you do this within the powershell commandlets only?

$lines = Get-Content Test.csv

$words = $lines[0].Split(",")

foreach ($word in $words){
       $newword += "`"" + $word + "`","
}
$newword = $newword.Trim(",")
$lines[0] = $newword

$lines

Free Windows Admin Tool Kit Click here and download it now
July 10th, 2013 3:03pm

Before we get too far into this, can you post the actual error message you're getting?
July 10th, 2013 3:11pm

It would definately be good to have more information regarding this such as the error and what the header actually looks like, but with that, this is one approach that should work:

$data = Get-Content file.csv
@($data[0] -split "," -replace '((?:\w+|\s)+)','"$1"' -join ",") + $data[1..$data.Count] | 
    Out-File file.csv

Free Windows Admin Tool Kit Click here and download it now
July 10th, 2013 3:55pm

HI Guys:

I think the problem i was having was the way I was using import-csv to import the file. I learned something new too but it was way overly complicated. I'll finish out the script and repost the exact scripts and error I get if I need anymore help. Thanks again for all your input!

July 10th, 2013 6:55pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics