export mail contact mail user and external contacts

Glenn Maxwell 12,001 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?

Microsoft Exchange Online
Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
1,399 questions
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,683 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,747 questions
Microsoft Exchange Hybrid Management
Microsoft Exchange Hybrid Management
Microsoft Exchange: Microsoft messaging and collaboration software.Hybrid Management: Organizing, handling, directing or controlling hybrid deployments.
2,191 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jake Zhang-MSFT 8,010 Reputation points Microsoft Vendor
    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 comments No comments

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.