0

Csv file data as below.

Header1,header2
Abc,xyz,pqr
Ab,pd,ss

When is use import-csv from powershell the last column pqr & ss is not getting imported.

I want output in this way.

Header1  header2
-----------------
Abc      xyz,pqr
Ab       pd,ss

How to have this imported?

3 Answers 3

3

Your CSV is malformed. If you are going to have a comma within a field that field needs to be enclosed in quotes.

Header1,header2
Abc,"xyz,pqr"
Ab,"pd,ss"

If you want that to be an array that will be loaded by powershell later and used as an array then you probably want to export the object as XML rather than a CSV since that supports nested arrays.

If this is a CSV that is generated by some other application that you are trying to load into PowerShell then you will need to parse the file yourself and probably won't be able to use Import-CSV. Something like:

$Imported = Get-Content $file | Select -Skip 1 | ForEach {[PSCustomObject][Ordered]@{'Header1'=$_.Split(",")[0];'Header2'=$_.Split(",")[1..2]}}

Alternatively you can store the CSV with a different delimiter so maybe it would look more like (semicolon delimited):

Header1;Header2
Abc;xyz,pqr
Ab;pd,ss
Sign up to request clarification or add additional context in comments.

Comments

1

use -header to provide a title for the columns, including the third column

After import concatenate columns 3 & 4 together

Comments

0

you can separate column 2 data with another delimiter like '-' or something else so that you can import it correctly

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.