user3175140
user3175140

Reputation: 531

Powershell - Get-DistributionGroup exporting to csv issue

The below command will output the correct information to the screen, however when exporting to a csv file it looks nothing like what is shown on the screen. I have been able to export with other cmdlets, Get-DistributionGroup seems to corrupt the data when using with export.csv.

 Outputting to screen works great!
 Get-DistributionGroup  | Where {$_.emailaddresses -like "*@adomain.com*"} | FT  Name,Alias,EmailAddresses

 Exporting to csv doesnt work correctly
 Get-DistributionGroup  | Where {$_.emailaddresses -like "*@adomain.com*"} | FT  Name,Alias,EmailAddresses | Export-csv C:/thisis.csv

Upvotes: 0

Views: 1819

Answers (2)

user3175140
user3175140

Reputation: 531

Thank you the answer has been resolved.

BartekB's answer

Get-DistributionGroup | Where {$.emailaddresses -like "*@adomain.com*"} | select Name,Alias, @{ Name = 'EmailAddresses' Expression = { $.EmailAddresses -join '|' } }

Upvotes: 0

BartekB
BartekB

Reputation: 8660

Instead of Format-Table (alias: ft) you should use Select-Object. Export-Csv expects objects as input, not formating instructions.

Format-Table, by definition, will convert objects to something that looks well in your output, but it's one way trip: you loose original objects as a part of the process.

Select-Object on the other hand can create objects with subset of properties, so that cmdlets like Export-Csv can still use data from original source.

EDIT

Thing I missed originally: you try to export a property that is a collection (EmailAddresses). That won't work unless you 'flatten' the collection first:

Get-DistributionGroup  | Where {$_.emailaddresses -like "*@adomain.com*"} | select Name,Alias, @{
    Name = 'EmailAddresses'
    Expression = { $_.EmailAddresses -join '|' }
}

Upvotes: 1

Related Questions