none
SOLVED: How to change the titles/headers in the export-csv result file RRS feed

  • Question

  • Hey, ps noobie here..


    How do I change the titles/headers in the .csv, when I use the export-csv?

    This is my script:

    $path = "\\server\script-$((get-date).tostring('dd-MM-yyyy')).csv"
    get-adgroupmember nameofgroup |
     get-aduser -properties name,sAMAccountName,company,department,description |
     select name,sAMAccountName,company,department,description |
     Sort-Object company |
    Export-Csv -delimiter ";" $path

    The headers are now the following:
    name,sAMAccountName,company,department,description

    What do i use to change, for example, samaccountname to "brukernavn"? (brukernavn means username in my language..)

    Thanks




    • Edited by PowerShellGirl Thursday, September 13, 2012 1:19 PM SOLVED
    Tuesday, September 4, 2012 8:57 PM

Answers

  • Another option, use calculated expressions with your Select statement:

    select name,@{n='brukernavn';e=$_.sAMAccountName},company,department,description


    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by PowerShellGirl Tuesday, September 11, 2012 8:08 AM
    Wednesday, September 5, 2012 3:11 AM
  • Typo.

    $path = "\\server\script-$((get-date).tostring('dd-MM-yyyy')).csv"
    get-adgroupmember nameofgroup |
     get-aduser -properties name,sAMAccountName,company,department,description |
      foreach {
                new-object psobject -Property @{
                                                 brukernavn = $_.sAMAccountName
                                                 Name       = $_.name
                                                 Company    = $_.company
                                                 Department = $_.department
                                                 Description = $_.description
                                                 }
               } | Select name,brukernavn,company,department,description |
                   Export-Csv -delimiter ";" $path


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by PowerShellGirl Tuesday, September 11, 2012 8:08 AM
    Tuesday, September 4, 2012 10:09 PM

All replies

  • Try this:

    $path = "\\server\script-$((get-date).tostring('dd-MM-yyyy')).csv"
    get-adgroupmember nameofgroup |
     get-aduser -properties name,sAMAccountName,company,department,description |%
      foreach {
                new-object psobject -Property @{
                                                 brukernavn = $_.sAMAccountName
                                                 Name       = $_.name
                                                 Company    = $_.company
                                                 Department = $_.department
                                                 Description = $_.description
                                                 }
               } | Select name,brukernavn,company,department,description |
                   Export-Csv -delimiter ";" $path
      
     Change the keys of the hash table to whatevery you want those column names to be in the csv, and then select them in whatever order you want in the select statement, by those names. 

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    • Edited by mjolinor Tuesday, September 4, 2012 9:27 PM
    Tuesday, September 4, 2012 9:18 PM
  • Thanks for your help! But I get a error.


    Missing opening '(' after keyword 'foreach'.
    At \\server\script.ps1 line:4 char:11
    +   foreach  <<<< {
        + CategoryInfo          : ParserError: (OpenParenToken:TokenId) [], ParseException
        + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword


    I dont get it.

    Tuesday, September 4, 2012 9:34 PM
  • Typo.

    $path = "\\server\script-$((get-date).tostring('dd-MM-yyyy')).csv"
    get-adgroupmember nameofgroup |
     get-aduser -properties name,sAMAccountName,company,department,description |
      foreach {
                new-object psobject -Property @{
                                                 brukernavn = $_.sAMAccountName
                                                 Name       = $_.name
                                                 Company    = $_.company
                                                 Department = $_.department
                                                 Description = $_.description
                                                 }
               } | Select name,brukernavn,company,department,description |
                   Export-Csv -delimiter ";" $path


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    • Marked as answer by PowerShellGirl Tuesday, September 11, 2012 8:08 AM
    Tuesday, September 4, 2012 10:09 PM
  • Another option, use calculated expressions with your Select statement:

    select name,@{n='brukernavn';e=$_.sAMAccountName},company,department,description


    Grant Ward, a.k.a. Bigteddy

    • Marked as answer by PowerShellGirl Tuesday, September 11, 2012 8:08 AM
    Wednesday, September 5, 2012 3:11 AM
  • I started with that, but then imagined what it was going to end up looking like after he translated all the other labels to native language. 

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Wednesday, September 5, 2012 3:16 AM
  • Hi,

    Any update about the issue?

    Regards,

    Yan Li

    TechNet Subscriber Support

    If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.


    Yan Li

    TechNet Community Support

    Monday, September 10, 2012 6:18 AM
    Moderator
  • Hey.

    I tried both solutions and both worked as expected. Thanks a lot for your help guys.

    select name,@{n='brukernavn';e=$_.sAMAccountName}

    I ended up using this^ solution, cause Im using another "foreach" in my script and I kind of fucked the script up when I tried using another "foreach". Probably cause I'm a noob. Anyways, this worked perfectly. Thank you.

    Tuesday, September 11, 2012 8:10 AM