exchange online : Export list of nested distribution group members in entire forest via powershell in csv format

Gurudas 886 Reputation points
2022-01-10T19:13:18.487+00:00

Hello Team,

Hope you are doing great!

I would like to know in exchange online how can we export list of nested distribution group members in entire forest via powershell in CSV format.

Thank you in advance :)

Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,197 questions
{count} votes

1 answer

Sort by: Most helpful
  1. KyleXu-MSFT 26,211 Reputation points
    2022-01-11T02:59:44.793+00:00

    @GurudasSatardekar

    Our forum is for questions and feedback related to Exchange server. Usually, we also help users modify their scripts but don't support writing scripts directly.

    I modified the following script with reference to this blog, it may by useful to you:
    Please Note: Since the web site is not hosted by Microsoft, the link may change without notice. Microsoft does not guarantee the accuracy of this information.

    ## Set Variables:  
        $members = New-Object System.Collections.ArrayList  
      
    ## Create the Function  
        function getMembership($group) {  
            $searchGroup = Get-DistributionGroupMember $group -ResultSize Unlimited  
            foreach ($member in $searchGroup) {  
                if ($member.RecipientTypeDetails-match "Group" -and $member.DisplayName -ne "") {  
                    getMembership($member.DisplayName)  
                    }             
                else {  
                    if ($member.DisplayName -ne "") {  
                        if (! $members.Contains($member.DisplayName) ) {  
                            $members.Add($member.DisplayName) >$null  
                            }  
                        }  
                    }  
                }  
            }  
       
    ## Run the function  
      
    $groups = Get-DistributionGroup -ResultSize unlimited  
      
    foreach ($group in $groups) {  
        write-host "`nGroup Name: " $group -ForegroundColor:Green  
        getMembership($group.DisplayName)  
        ## Output results to screen  
        write-host "Group Members:" -ForegroundColor:Yellow  
        $members.GetEnumerator() | sort-object  
        $members = New-Object System.Collections.ArrayList  
    }  
    

    Here are group information for my Exchange online lab:
    163774-qa-kyle-10-46-06.png
    163756-qa-kyle-10-46-36.png
    163650-qa-kyle-10-47-01.png

    Here is the output of this script:
    163757-qa-kyle-10-51-03.png

    If you want to export the result, you need to replace this part below:

    foreach ($group in $groups) {  
        $group  
        getMembership($group.DisplayName)  
        $members.GetEnumerator() | sort-object  
        $members = New-Object System.Collections.ArrayList  
    }  
    

    Then run this script as the picture below:
    163729-qa-kyle-10-55-11.png

    If you want to export the results into CSV file, you may need to open a ticket to Microsoft for a more professional scripting support.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


    0 comments No comments