Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.


Go Back   Vista Forums > Misc Newsgroups > PowerShell

RB

Vista - CSV headers

Reply
 
12-14-2007   #1
PSApple


 
 

CSV headers

There has to be an easy way to grab all the header names from a import-csv?

ex: myfile.csv
code,name,state,zip
34,mike,co,80525
68,jim,co,80528

and do $mydata= import-csv myfile.csv, how do I get out the names of the
headers
I'm tring to get the equivilant of $headNames= $("code",name","State","Zip")





My System SpecsSystem Spec
12-14-2007   #2
Shay Levi


 
 

Re: CSV headers

Hi PSApple,

Each header is represneted by a NoteProperty member, you can use get-member
to list them:


$csv = import-csv myfile.csv
$headres = $csv | Get-Member -MemberType NoteProperty | foreach {$_.name}
$headres


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> There has to be an easy way to grab all the header names from a
> import-csv?
>
> ex: myfile.csv
> code,name,state,zip
> 34,mike,co,80525
> 68,jim,co,80528
> and do $mydata= import-csv myfile.csv, how do I get out the names of
> the
> headers
> I'm tring to get the equivilant of $headNames=
> $("code",name","State","Zip")

My System SpecsSystem Spec
12-14-2007   #3
Shay Levi


 
 

Re: CSV headers

Small typo:

$headres -shouldBe $headers

-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic


Quote:

> Hi PSApple,
>
> Each header is represneted by a NoteProperty member, you can use
> get-member to list them:
>
> $csv = import-csv myfile.csv
> $headres = $csv | Get-Member -MemberType NoteProperty | foreach
> {$_.name}
> $headres
> -----
> Shay Levi
> $cript Fanatic
> http://scriptolog.blogspot.com
> Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
Quote:

>> There has to be an easy way to grab all the header names from a
>> import-csv?
>>
>> ex: myfile.csv
>> code,name,state,zip
>> 34,mike,co,80525
>> 68,jim,co,80528
>> and do $mydata= import-csv myfile.csv, how do I get out the names of
>> the
>> headers
>> I'm tring to get the equivilant of $headNames=
>> $("code",name","State","Zip")

My System SpecsSystem Spec
12-14-2007   #4
Brandon Shell [MVP]


 
 

Re: CSV headers

You could use something like this.

Import-Csv C:\Data\Scripts\testme.txt | get-member -type properties |
foreach-object{$_.name}

When you use import-csv it creates and object using the CSV headers as the
properties and the values are attained from the csv.

"PSApple" <mapplebee@xxxxxx> wrote in message
news:u$PESVlPIHA.748@xxxxxx
Quote:

> There has to be an easy way to grab all the header names from a
> import-csv?
>
> ex: myfile.csv
> code,name,state,zip
> 34,mike,co,80525
> 68,jim,co,80528
>
> and do $mydata= import-csv myfile.csv, how do I get out the names of the
> headers
> I'm tring to get the equivilant of $headNames=
> $("code",name","State","Zip")
>
>
>
>
My System SpecsSystem Spec
12-14-2007   #5
Umesh Thakur


 
 

RE: CSV headers

try this:
$mydata= import-csv myfile.csv
$mydata | gm | Where-Object { $_.memberType -eq "NoteProperty" } |
Select-Object name

when you import-csv, PS will add the fields as member of array $mydata and
they will be of "NoteProperty" type.

--
Umesh

"Old programmers never die. They just terminate and stay resident."



"PSApple" wrote:
Quote:

> There has to be an easy way to grab all the header names from a import-csv?
>
> ex: myfile.csv
> code,name,state,zip
> 34,mike,co,80525
> 68,jim,co,80528
>
> and do $mydata= import-csv myfile.csv, how do I get out the names of the
> headers
> I'm tring to get the equivilant of $headNames= $("code",name","State","Zip")
>
>
>
>
>
My System SpecsSystem Spec
12-14-2007   #6
Kiron


 
 

Re: CSV headers

$headNames = (gc myfile.csv -t 1).split(',') -replace '^\s'

--
Kiron
My System SpecsSystem Spec
Reply

RB


Thread Tools


Similar Threads for: CSV headers
Thread Forum
Headers in WLM Live Mail
How to see headers Live Mail
Headers Only Live Mail
headers... Vista mail
Headers Vista mail


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd