Is it possible to read a CSV if the headers have spaces between them ?

CSV contains the following headers:
Last Name,First Name,Dept Descrip,Social Security Numbers,Employment Category,Job Title,Work Phone,Room,Clock Number,Date Of Birth,Div Descrip

Open in new window

Or I have to manually change it to be like:
LastName,FirstName,DeptDescrip,SocialSecurityNumbers,EmploymentCategory,JobTitle,WorkPhone,Room,ClockNumber,DateOfBirth,DivDescrip

Open in new window

Thanks,
namergSystems AdministratorAsked:
Who is Participating?
 
apathy42Connect With a Mentor Commented:
Yes, you can.  To access the items, you would need to do something like this:

$data = Import-CSV filename.csv

foreach ($line in $data) {
 $lastname = $data."Last Name"
 $firstname = $data."First Name"
}

...and so on.  I tested it, this works for me.
0
 
QlemoConnect With a Mentor Batchelor and DeveloperCommented:
Since the data is comma separated, spaces will not harm. But you'll have to use quotes whenever you refer to properties containing special chars:
@"
Name One,Name Two
one, two
"@ | ConvertFrom-Csv | % { $_.'Name One' }

Open in new window

or
@"
Name One,Name Two
one, two
"@ | ConvertFrom-Csv | % { $_.{Name One} }

Open in new window

0
 
namergSystems AdministratorAuthor Commented:
And what if I want to display it through a write-output like

write-output "Last name: $($file2_user.LastName)"

Thanks,
PS: Never mind, I got it.
0
 
namergSystems AdministratorAuthor Commented:
@apathy42: Thank you very much.
@Qlemo: You are way too advanced for me :) Your shortcuts in the code hard to get for me :( sorry.
0
 
QlemoBatchelor and DeveloperCommented:
@" and "@  build a multi-line string, and the combination with convertfrom-csv has been used here to build a closed example only.
% is the same as foreach-object.
0
All Courses

From novice to tech pro — start learning today.