Share via

Export external users

Rising Flight 6,456 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"

Exchange Online
Exchange Online

A cloud-based service included in Microsoft 365, delivering scalable messaging and collaboration features with simplified management and automatic updates.

Exchange | Exchange Server | Other
Exchange | Exchange Server | Other

A robust email, calendaring, and collaboration platform developed by Microsoft, designed for enterprise-level communication and data management.Miscellaneous topics that do not fit into specific categories.

Exchange | Exchange Server | Management
Exchange | Exchange Server | Management

The administration and maintenance of Microsoft Exchange Server to ensure secure, reliable, and efficient email and collaboration services across an organization.

Exchange | Hybrid management
Exchange | Hybrid management

The administration of a hybrid deployment that connects on-premises Exchange Server with Exchange Online, enabling seamless integration and centralized control.

0 comments No comments

Answer accepted by question author

Anonymous
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

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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