Solved

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

Posted on 2013-01-15
5
333 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now