Export external users

Rising Flight 4,816 Reputation points
2025-01-30T09:41:42.78+00:00

I want to export unified groups that have external users or contacts added to them. Please help me with the script. I found the script below in a forum and modified it, but I have not tested it yet. Please guide me.

# Import the CSV file containing unified group names
$UGList = Import-Csv -Path "c:\temp\input.csv"
if ($UGList.Count -eq 0) {
    Write-Host "No data found in the CSV file."
    exit
}

$UnifiedGroupsWithExternalUsers = @()
$totalUGs = $UGList.Count
$currentUG = 0

foreach ($UG in $UGList) {
    $currentUG++
    Write-Progress -Activity "Processing Distribution Groups" -Status "$currentUG out of $totalUGs" -PercentComplete (($currentUG / $totalUGs) * 100)

    # Fetch the Unified Group
    $Group = Get-UnifiedGroup -Identity $UG.UGAlias -ResultSize Unlimited

    if ($Group) {
        # Fetch the members and filter external users
        $Members = Get-UnifiedGroupLinks -Identity $Group.Identity -LinkType Members -ResultSize Unlimited | Where-Object {
            $_.RecipientType -eq 'MailContact' -or $_.RecipientType -eq 'MailUser' -or $_.ExternalEmailAddress -ne $null
        }

        if ($Members) {
            foreach ($Member in $Members) {
                $UnifiedGroupsWithExternalUsers += [PSCustomObject]@{
                    GroupName   = $Group.DisplayName
                    MemberName  = $Member.DisplayName
                    MemberType  = $Member.RecipientType
                    MemberEmail = $Member.PrimarySmtpAddress
                }
            }
        }
    } else {
        Write-Host "Warning: Unified Group '$($UG.UGAlias)' not found."
    }
}

# Export the results to a CSV file
$UnifiedGroupsWithExternalUsers | Export-Csv -Path "C:\temp\output.csv" -NoTypeInformation
Write-Host "Export completed. File saved to C:\temp\output.csv"

Microsoft Exchange Online
Exchange Server
Exchange Server
A family of Microsoft client/server messaging and collaboration software.
1,435 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,734 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,787 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,219 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jake Zhang-MSFT 8,935 Reputation points Microsoft Vendor
    2025-01-31T01:47:22.7733333+00:00

    Hi @Rising Flight ,

    Welcome to the Microsoft Q&A platform!

    Based on your description, your script looks well-structured! Here are some suggestions to ensure it runs smoothly:

    1. Make sure you have installed and imported the necessary modules, such as ExchangeOnlineManagement.
    2. Add error handling to manage any unexpected issues.
    3. Test the script with a small subset of data to verify its functionality before running it on the entire dataset.

    Here is a slightly improved version of your script with error handling added:

    # Import the CSV file containing unified group names
    $UGList = Import-Csv -Path "c:\temp\input.csv"
    if ($UGList.Count -eq 0) {
        Write-Host "No data found in the CSV file."
        exit
    }
    
    $UnifiedGroupsWithExternalUsers = @()
    $totalUGs = $UGList.Count
    $currentUG = 0
    
    foreach ($UG in $UGList) {
        $currentUG++
        Write-Progress -Activity "Processing Distribution Groups" -Status "$currentUG out of $totalUGs" -PercentComplete (($currentUG / $totalUGs) * 100)
    
        try {
            # Fetch the Unified Group
            $Group = Get-UnifiedGroup -Identity $UG.UGAlias -ResultSize Unlimited
    
            if ($Group) {
                # Fetch the members and filter external users
                $Members = Get-UnifiedGroupLinks -Identity $Group.Identity -LinkType Members -ResultSize Unlimited | Where-Object {
                    $_.RecipientType -eq 'MailContact' -or $_.RecipientType -eq 'MailUser' -or $_.ExternalEmailAddress -ne $null
                }
    
                if ($Members) {
                    foreach ($Member in $Members) {
                        $UnifiedGroupsWithExternalUsers += [PSCustomObject]@{
                            GroupName   = $Group.DisplayName
                            MemberName  = $Member.DisplayName
                            MemberType  = $Member.RecipientType
                            MemberEmail = $Member.PrimarySmtpAddress
                        }
                    }
                }
            } else {
                Write-Host "Warning: Unified Group '$($UG.UGAlias)' not found."
            }
        } catch {
            Write-Host "Error processing group '$($UG.UGAlias)': $_"
        }
    }
    
    # Export the results to a CSV file
    $UnifiedGroupsWithExternalUsers | Export-Csv -Path "C:\temp\output.csv" -NoTypeInformation
    Write-Host "Export completed. File saved to C:\temp\output.csv"
    

    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

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.