Exporting PSObject containing Distribution Group membership details to .csv file

Exchange 2013 | Windows Server 2012 R2 Standard

Hello Experts,

I have written a script to show all Exchange 2013 Distribution Group's name and membership:

$dgs = Get-DistributionGroup
$results = @()

foreach ($dg in $dgs) {
    $members = Get-DistributionGroupMember $dg.primarysmtpaddress
    $object = New-Object -TypeName PSObject
    $object | Add-Member -MemberType NoteProperty -Name 'DG Name' -Value $dg.DisplayName
    $object | Add-Member -MemberType NoteProperty -Name 'Member Name' -Value $members.DisplayName
    $results += $object

It works ok when written to the host except if the membership of the DG is more than about 5 mailboxes the names end with three dots.  Example:

Member Name
{User A, User B, User C, User D...}

The entire membership list isn't shown so I tried exporting the results to a .csv file:

$results | Select-Object 'DG Name','Member Name' | Export-Csv DG.csv -NoTypeInformation

That resulted in a .csv file with 'System.Object[]' listed under the 'Member Name' column for each DG member.  What am I doing wrong here?  Thanks in advance!
LVL 17
Jason CrawfordTransport NinjaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The data doesn't lend itself well to a .CSV format (because of the varying number of members possible in a group).  You could just output to a list format (pipe to Format-List).

If you really, really want a .CSV, then you need to join all the objects of the array (which is what your "Member Name" property is) into a single string.  You can delimit that string with a useful character like ";" or a newline.
$object | Add-Member -MemberType NoteProperty -Name 'Member Name' -Value "$($members.DisplayName -join ';')"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.