Unable to export to a Excel file or CSV with the PowerShell cmdlet Export-Csv

Viewing 6 reply threads
  • Author
    Posts
    • #1557380
      IQ
      Participant
      Member Points: 721
      Rank: 2

      How to export the output of the Get-ADUser cmdlet to an Excel file? Excel-CSV is not showing the output as expected. It’s showing ASCII characters. On the other hand, if I use Out-File C:\ADUsers.txt  to generate a txt file it works fine. But I need to generate a CSV or an Excel file. How can I do this?

      I am using  Export-Csv -Path  c:\temp\UserData.csv.

      import-module activedirectory
      Get-ADUser -Filter * -Properties * |
      sort-object -property SamAccountName |
      Format-Table -property `
      @{Name="Logon Name";Expression={$_.SamAccountName}},
      @{Name="First Name";Expression={$_.givenName}},
      @{Name="Initials";Expression={$_.Initials}},
      @{Name="Last Logon";Expression={$_.LastLogonDate}}`
      -AutoSize |
      Out-File C:\ADUsers.txt
      avatar
    • #1557381
      Leos Marek
      Moderator
      Member Points: 22,833
      Author of Year 2020
      Rank: 4

      Replace Format-table with Select-object and you are all set.
      any formatting using format commands is for display only.

      avatar
    • #1557382
      Leos Marek
      Moderator
      Member Points: 22,833
      Author of Year 2020
      Rank: 4

      oh and remove the -autosize parameter

      avatar
    • #1557383
      IQ
      Participant
      Member Points: 721
      Rank: 2

      Excellent help, Leos. You are a real Pro, it works like a charm. Great help. Thank you so much.

    • #1557394
      Jake Blues
      Participant
      Member Points: 101
      Rank: 2

      What’s wrong with the ImportExcel module? (It can do all sorts of Excel things…..)

    • #1557395
      Leos Marek
      Moderator
      Member Points: 22,833
      Author of Year 2020
      Rank: 4

      Hehe thanks 🙂 Your welcome.

      I had the same issue few days ago when I audited few PCs… 🙂 Just did all on screen with format-table and when I was happy I just added | Export-CSV. It took me a while before I realized I forgot to remove the format thing 🙂

      Format commands are only used as last commands to display results, as they completely change the member type.

    • #1557396
      Leos Marek
      Moderator
      Member Points: 22,833
      Author of Year 2020
      Rank: 4

      Noone said there is something wrong with the module you share 🙂 The question was about native export commands which is much easier and faster than learn how to work with a specific module.

Viewing 6 reply threads
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account