Microsoft Exchange Online
A Microsoft email and calendaring hosted service.
2,091 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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"
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:
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