none
Adding headers to a new file or csv

    Question

  • G'day!

    I'm looking for an easy way to add headers to a "Out-file" file like export-csv does.

    Right now the -append option appends everything nicely, but without headers.

    The file can be newly created at the start of the script, after which a 'foreach' will fill it with content.

    Thanks in advance!


    "Please contact your administrator..." :-(

    Tuesday, May 15, 2012 2:15 PM

Answers

  • you can do this by creating a new object and feed your data in it.

    Afterwards you can use the out-file cmdlet to output your object to a file.

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value "value" #Name is your header

    $obj | Add-Member -MemberType NoteProperty -Name "Place" -Value "value" #Place is your header

    $obj | out-file -filepath "path"

    What kind of information would you like to add to the object? Is it a string, array or hash table?

    regards

    Powershell Junior










    • Edited by King Julien Tuesday, May 15, 2012 2:35 PM
    • Marked as answer by Ed-man Monday, May 21, 2012 1:55 PM
    Tuesday, May 15, 2012 2:24 PM
  • with foreach a loop:

    $b = 1,2,3,4
     
    $array = @()
     
    foreach ($item in $b) {
     
    $obj = new-object psobject
     
    $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $item
     
    $array += $obj
     
    }
     
    $array
    


    • Edited by King Julien Tuesday, May 15, 2012 2:58 PM
    • Marked as answer by Ed-man Wednesday, May 16, 2012 1:50 PM
    Tuesday, May 15, 2012 2:57 PM

All replies

  • you can do this by creating a new object and feed your data in it.

    Afterwards you can use the out-file cmdlet to output your object to a file.

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value "value" #Name is your header

    $obj | Add-Member -MemberType NoteProperty -Name "Place" -Value "value" #Place is your header

    $obj | out-file -filepath "path"

    What kind of information would you like to add to the object? Is it a string, array or hash table?

    regards

    Powershell Junior










    • Edited by King Julien Tuesday, May 15, 2012 2:35 PM
    • Marked as answer by Ed-man Monday, May 21, 2012 1:55 PM
    Tuesday, May 15, 2012 2:24 PM
  • What about:

    $in = Get-content c:\temp\a.txt '"header1","hearder2","header3"',$in |`

    out-file c:\temp\result.csv -notypeinformation -encoding ASCII

    This will add a header line to your data, and create a new file

    You can also use encoding UTF8 if you prefer


    Life is short, Enjoy it now. Cyreli






    • Edited by Cyreli Tuesday, May 15, 2012 2:59 PM
    • Edited by Yan Li_Moderator Wednesday, May 16, 2012 4:24 AM full it
    Tuesday, May 15, 2012 2:42 PM
  • Only strings. Export-csv worked fine, until I added an array-type property, so I had to define all properties one by one and build the file again. But that left me without headers. Comes down to something like this: ($name + ";" + $alias + ";" + $languages) where $languages is the isolated array-property. Derived from get-mailbox...

    "Please contact your administrator..." :-(

    Tuesday, May 15, 2012 2:52 PM
  • with foreach a loop:

    $b = 1,2,3,4
     
    $array = @()
     
    foreach ($item in $b) {
     
    $obj = new-object psobject
     
    $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $item
     
    $array += $obj
     
    }
     
    $array
    


    • Edited by King Julien Tuesday, May 15, 2012 2:58 PM
    • Marked as answer by Ed-man Wednesday, May 16, 2012 1:50 PM
    Tuesday, May 15, 2012 2:57 PM
  • Hi Ed,
    Honestly I don't understand what you need. Please give some examples.

    These are only my guesses. If you have couple variables like this: ($name + ";" + $alias + ";" + $languages)
    And you need header to it then maybe try bind it to one object or object collections and next this export to file.

    $object = New-Object PSObject -Property @{
     Name = $name
     Alias = $alias
     Languages = $languages | Out-String  #to convert array to string
    }
    $object | Export-csv "c:\test.csv"
    

    Wednesday, May 16, 2012 7:08 AM
  • Hello there!

    I finally solved my issue  using a combination of the mentioned solutions. This is what finally worked for me, part of the script:

    $b = Get-Mailbox -DomainController $dc -ResultSize unlimited |select Name,SamAccountName,Alias,DisplayName,PrimarySmtpAddress,RecipientTypeDetails,Languages,Emailaddresses

    $b2 = $b |where {$_.recipienttypedetails -like "UserMailbox"}|select Name,SamAccountName,Alias,DisplayName,PrimarySmtpAddress,RecipientTypeDetails,Languages

    $output = @()

    ForEach ($i in $b2)

    {

    $name = $i.name

    $sam = $i.samaccountname

    $alias = $i.alias

    $smtp = $i.primarysmtpaddress

    $display = $i.displayname

    $language = $i.languages

    ForEach ($lang in $language)

    {

    $lng = $lang.name

    $obj = new-object psobject

    $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $name

    $obj | Add-Member -MemberType NoteProperty -Name "SamAccountName" -Value $sam

    $obj | Add-Member -MemberType NoteProperty -Name "Alias" -Value $alias

    $obj | Add-Member -MemberType NoteProperty -Name "PrimarySmtpAddress" -Value $smtp

    $obj | Add-Member -MemberType NoteProperty -Name "DisplayName" -Value $display

    $obj | Add-Member -MemberType NoteProperty -Name "Languages" -Value $lng

    $output += $obj

    }

    }

    $output | Export-Csv ($path + "UserMailboxExportLang.csv") -Delimiter ";" -NoClobber

    I guess it can be built in a more clever way, but I got what I was looking for. Thanks guys!


    "Please contact your administrator..." :-(

    Monday, May 21, 2012 2:04 PM