SOLVED

powershell: how to get members of a distribution list

Brass Contributor

Hi all,

my company have several domains under its tenant.

 

Now I should get, for each domain under the tenant, the list of the members of every dsitribution list.

 

if too hard, for me would be fine to get the members of a distribution list of a given domain

 

do you also know how I can choose the output values? I mean, mailbox or other info of the members

 

 

thanks!

 

 

3 Replies

@mfranhind115 

 

If it's just a case of multiple domains within a single tenant, you don't have to do anything special for this. It would be a bit more involved if you had to do this across multiple tenants.

 

Here's a basic template to get you started that uses the Microsoft ExchangeOnline V2 module.

 

 

Note: I've deliberately specified a limit using "ResultSize 10" on line 1 as this isn't a quick process, and starting off with "ResultSize unlimited" may not be wise. But feel free to change the value as you see fit.

 

Get-ExoDistributionListMembers.ps1

 

Get-EXORecipient -RecipientType MailUniversalDistributionGroup, DynamicDistributionGroup -Properties ExchangeGuid, ExternalDirectoryObjectId -ResultSize 10 |
    ForEach-Object {
        $Group = $_;

        # Set up the common attributes now so that we're not doing it multiple times (per group type) later.
        $Output = @{
            Id = $Group.ExchangeGuid;
            AadId = $Group.ExternalDirectoryObjectId;
            GroupType = $Group.RecipientType;
            GroupName = $Group.Name;
            GroupPrimarySmtpAddress = $Group.PrimarySmtpAddress;
            GroupEmailAddresses = $Group.EmailAddresses;
            Members = [System.Collections.Generic.List[PSCustomObject]]::new();
        }

        switch ($Group.RecipientType)
        {
            "MailUniversalDistributionGroup" {
                $null = $Output.Members.Clear();

                Get-DistributionGroupMember -Identity ($Group.Identity) |
                    ForEach-Object {
                        $null = $Output.Members.Add([PSCustomObject] @{
                            MemberId = $_.ExchangeGuid;
                            MemberAadId = $_.ExternalDirectoryObjectId;
                            MemberType = $_.RecipientType;
                            MemberAddress = $_.PrimarySmtpAddress;
                        });
                    }

                [PSCustomObject] $Output;
                break;
            }

            "DynamicDistributionGroup" {
                $null = $Output.Members.Clear();

                Get-DynamicDistributionGroupMember -Identity ($Group.Identity) |
                    ForEach-Object {
                        $null = $Output.Members.Add([PSCustomObject] @{
                            MemberId = $_.ExchangeGuid;
                            MemberAadId = $_.ExternalDirectoryObjectId;
                            MemberType = $_.RecipientType;
                            MemberAddress = $_.PrimarySmtpAddress;
                        });
                    }

                [PSCustomObject] $Output;
                break;
            }
        }
    }

 

 

Cheers,

Lain

best response confirmed by mfranhind115 (Brass Contributor)
Solution

I should have added this before posting above.

 

If you wanted to flatten the output into a very basic CSV file rather than do further work with the objects, you can do it like this:

 

.\Get-ExoDistributionListMembers.ps1 |
    ForEach-Object {
        $Group = $_;

        $Group.Members |
            ForEach-Object {
                $_ | Select-Object -Property @{n="GroupType"; e={ $Group.GroupType}}, @{n="GroupName"; e={ $Group.GroupName}}, MemberType, MemberAddress;
            }
    } | Export-Csv -Path ".\distributionLists.csv" -NoTypeInformation;

 

Cheers,

Lain

thank you so much LAin!!!

appreciate!
1 best response

Accepted Solutions
best response confirmed by mfranhind115 (Brass Contributor)
Solution

I should have added this before posting above.

 

If you wanted to flatten the output into a very basic CSV file rather than do further work with the objects, you can do it like this:

 

.\Get-ExoDistributionListMembers.ps1 |
    ForEach-Object {
        $Group = $_;

        $Group.Members |
            ForEach-Object {
                $_ | Select-Object -Property @{n="GroupType"; e={ $Group.GroupType}}, @{n="GroupName"; e={ $Group.GroupName}}, MemberType, MemberAddress;
            }
    } | Export-Csv -Path ".\distributionLists.csv" -NoTypeInformation;

 

Cheers,

Lain

View solution in original post