Search code examples
powershellimport-csv

Import-CSV outputs the header row as a single property


I have run into a really annoying problem involving trying to get a specific cell from a csv file using Import-CSV.

I have followed the instructions as far as other answers have shown me: Let's say we have a csv file with columns named col1, col2, col3, col4 and so on

$importedCSV = Import-CSV csvfile.csv
$importedCSV
col1;col2;col3;col4
-------------------
itm1;itm2;itm3;itm4
itm5;itm6;itm7;itm8

It seems to work well so far, and if I do $importedCSV[0] and $importedCSV[1] it will filter out the respective non-selected row like normal.

The problem arises when I try to call a specific cell like $importedCSV[0].col2, it wont display anything at all, and if I press tab after the dot, it autocompletes as 'col1;col2;col3;col4', if I do $importedCSV | Get-Member, it also displays 'col1;col2;col3;col4' as a single string property, I assume this is what makes it impossible to get a specific cell from the csv file.


Solution

  • I believe I found out what the issue was, I'm using Spanish Excel to generate my CSVs which uses ; instead of , as delimiter, the Delimiter has to be manually set to ; in the powershell command like so:

    Import-Csv -Path .\csvfile.csv -Delimiter ";"
    

    That solves the issue.