export mail contact mail user and external contacts

Glenn Maxwell 12,876 Reputation points
2025-01-10T06:50:37.9933333+00:00

Hi All,

I have distribution lists (DLs) created in both Exchange Online and Exchange On-Premises. These DLs include mail contacts, mail users, and external users. Some DLs are created in Exchange Online, while others are created in Exchange On-Premises.

I want to export all the DLs that contain mail contacts, mail users, or external users to a CSV file so that I can remove them from the DLs.

Could you please help me with a PowerShell script to fetch this information from both DLs and Unified Groups?

Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,178 questions
Exchange | Exchange Server | Other
Exchange | Exchange Server | Management
Exchange | Hybrid management
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2025-01-10T08:30:54.22+00:00

    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:

    1. Connect to Exchange Online:
    $UserCredential = Get-Credential
    Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName -Password $UserCredential.Password
    
    1. 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):

    1. Connect to Exchange Online:
    $UserCredential = Get-Credential
    Connect-ExchangeOnline -UserPrincipalName $UserCredential.UserName -Password $UserCredential.Password
    
    1. 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


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.