PowerShell for Windows Admins

Jul 16 2013   12:51PM GMT

CSV fields with spaces in the field name

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

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”
}

1  Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.
  • TomLiotta
    Is it necessary to know ahead of time that spaces are embedded? -- Tom
    125,585 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: