Connect to Exchange Online PowerShell
$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session
Get list of all distribution groups
$groups = Get-DistributionGroup -ResultSize unlimited
Loop through each group and get its members
foreach ($group in $groups) {
Write-Host “Group: $($group.Name)”
$members = Get-DistributionGroupMember -Identity $group.Name -ResultSize unlimited
foreach ($member in $members) {
Write-Host “Member: $($member.Name)”
}
}
Disconnect from Exchange Online PowerShell
Remove-PSSession $Session
Summary
This script first connects to Exchange Online PowerShell using the New-PSSession cmdlet and your Office 365 credentials. It then uses the Get-DistributionGroup cmdlet to get a list of all distribution groups in your organization.
For each group, the script uses the Get-DistributionGroupMember cmdlet to get a list of its members, including members of any nested distribution groups. It then loops through each member and writes their name to the console.
Finally, the script disconnects from Exchange Online PowerShell using the Remove-PSSession cmdlet.
Note that this script assumes that you are using Exchange Online PowerShell to manage your Office 365 distribution groups. If you are using an on-premises Exchange Server, you may need to modify the script to use the appropriate cmdlets and connection settings.
Leave a Reply