• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

CSV headers

P

PSApple

#1
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 Computer

S

Shay Levi

#2
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



> 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 Computer

S

Shay Levi

#3
Small typo:

$headres -shouldBe $headers

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



> 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

>> 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 Computer

B

Brandon Shell [MVP]

#4
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

> 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 Computer

U

Umesh Thakur

#5
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:

> 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 Computer