Categories

CSV fields with spaces in the field name

CSV files are one way we can get data into our scripts – but sometimes when we get given a CSV file it has spaces in the field name e.g.

"Area Number"
1
2
3
2
3
1
5

Dealing with this can be a bit awkward but you can access the field like this:

Import-Csv area.csv |
foreach {
$_.'Area Number'
}

Double quotes work as well

The trick comes if you need to add the field into a string

Import-Csv area.csv |
foreach {
"Area = " + $_.'Area Number' + " is available"
}

String concatenation works. Alternatively, if you want to use string substitution:

Import-Csv area.csv |
foreach {
"Area = $($_.'Area Number') is available"
}

Leave a Reply