Windows Vista Forums

CSV headers

  1. #1


    PSApple Guest

    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

  2. #2


    Shay Levi Guest

    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



    > 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

  3. #3


    Shay Levi Guest

    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



    > 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 System SpecsSystem Spec

  4. #4


    Brandon Shell [MVP] Guest

    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

    > 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

  5. #5


    Umesh Thakur Guest

    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:

    > 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

  6. #6


    Kiron Guest

    Re: CSV headers

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

    --
    Kiron

      My System SpecsSystem Spec


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