Solved

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

Posted on 2013-01-15
5
334 Views
Last Modified: 2013-01-15
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,
0
Comment
Question by:namerg
  • 2
  • 2
5 Comments
 
LVL 11

Accepted Solution

by:
apathy42 earned 250 total points
ID: 38779014
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
 
LVL 69

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 38779016
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
 

Author Comment

by:namerg
ID: 38779030
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
 

Author Comment

by:namerg
ID: 38779055
@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
 
LVL 69

Expert Comment

by:Qlemo
ID: 38779324
@" 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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question