Hi @Glenn Maxwell ,
Welcome to the Microsoft Q&A platform!
Below is a PowerShell script that you can use to fetch distribution lists (DLs) containing mail contacts, mail users, or external users from both Exchange Online and Exchange On-Premises, and export the information to a CSV file.
For Exchange Online:
- Connect to Exchange Online:
$UserCredential = Get-Credential
Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName -Password $UserCredential.Password
- Fetch DLs and export to CSV:
$DLs = Get-DistributionGroup -ResultSize Unlimited
$DLsWithExternalUsers = @()
foreach ($DL in $DLs) {
$Members = Get-DistributionGroupMember -Identity $DL.Identity
foreach ($Member in $Members) {
if ($Member.RecipientType -eq 'MailContact' -or $Member.RecipientType -eq 'MailUser' -or $Member.ExternalEmailAddress) {
$DLsWithExternalUsers += [PSCustomObject]@{
GroupName = $DL.DisplayName
MemberName = $Member.DisplayName
MemberType = $Member.RecipientType
}
}
}
}
$DLsWithExternalUsers | Export-Csv -Path "C:\DLsWithExternalUsers_ExchangeOnline.csv" -NoTypeInformation
For Exchange On-Premises:
Open Exchange Management Shell and run:
$DLs = Get-DistributionGroup -ResultSize Unlimited
$DLsWithExternalUsers = @()
foreach ($DL in $DLs) {
$Members = Get-DistributionGroupMember -Identity $DL.Identity
foreach ($Member in $Members) {
if ($Member.RecipientType -eq 'MailContact' -or $Member.RecipientType -eq 'MailUser' -or $Member.ExternalEmailAddress) {
$DLsWithExternalUsers += [PSCustomObject]@{
GroupName = $DL.DisplayName
MemberName = $Member.DisplayName
MemberType = $Member.RecipientType
}
}
}
}
$DLsWithExternalUsers | Export-Csv -Path "C:\DLsWithExternalUsers_ExchangeOnPremises.csv" -NoTypeInformation
For Unified Groups (Office 365 Groups):
- Connect to Exchange Online:
$UserCredential = Get-Credential
Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName -Password $UserCredential.Password
- Fetch Unified Groups and export to CSV:
$UnifiedGroups = Get-UnifiedGroup -ResultSize Unlimited
$UnifiedGroupsWithExternalUsers = @()
foreach ($Group in $UnifiedGroups) {
$Members = Get-UnifiedGroupLinks -Identity $Group.Identity -LinkType Members
foreach ($Member in $Members) {
if ($Member.RecipientType -eq 'MailContact' -or $Member.RecipientType -eq 'MailUser' -or $Member.ExternalEmailAddress) {
$UnifiedGroupsWithExternalUsers += [PSCustomObject]@{
GroupName = $Group.DisplayName
MemberName = $Member.DisplayName
MemberType = $Member.RecipientType
}
}
}
}
$UnifiedGroupsWithExternalUsers | Export-Csv -Path "C:\UnifiedGroupsWithExternalUsers.csv" -NoTypeInformation
This script will help you identify and export the distribution lists and unified groups containing mail contacts, mail users, or external users to CSV files.
Please feel free to contact me for any updates. And if this helps, don't forget to mark it as an answer.
Best,
Jake Zhang