Search code examples
powershellcsvexportreportexchange-server

How to Properly Export CSV for Get-Mail Results?


I am trying to export data of when email accounts have been created. The following code is what I am using:

Get-Mailbox -ResultSize unlimited | Format-Table DisplayName, whencreatedUTC | Export-Csv -Path C:\Desktop\Reports\test.csv

Without the export cmd, the results come in just fine in PS. However, when the export cmd is added, my results turn out blank in the exported .csv file. Any ideas what I am doing wrong?


Solution

  • Instead of using Format-Table, use Select-Object. PowerShell treats all data as objects, but the Format-* cmdlets are for displaying the output, not for further processing. Select-Object takes the input (from the pipeline, in this case), and generates a new object with only the selected members, which you can then pass to Export-CSV for saving. You should also add -NoTypeInformation to the Export-CSV command, or you'll get a broken CSV with an extra line at the beginning.