@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:
Here is the output of this script:
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:
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.