How to select first column of CSV file without column name in Powershl

Viewed 16

Having issues selecting a specific column within a .CSV file in powershell without using the column name. Basically, I have a 5 column spreadsheet where I want to grab the values within the first column but do not want to rely on the column name as it might defer if I use another spreadsheet.

Is there anyway of selecting a column by number or index value and NOT name?

1 Answers

Inspect the properties on the object parsed from the first row by accessing the hidden psobject memberset:

$firstColumnName = $null

Import-Csv whoKnows.csv |ForEach-Object {
    if($null -eq $firstColumnName){
        # first row, grab the first column name
        $firstColumnName = @($_.psobject.Properties)[0].Name
    }

    # access the value in the given column
    $_.$firstColumnName
}
Related