Forum Discussion
Fromelard
Oct 12, 2022Steel Contributor
Office 365 - Simple PowerShell script to refresh members of an Exchange Distribution Group
When you are in charge to support end user into their daily activities, the Distribution Group is a basic option. In many case, the number of members can change regularly and do that with Outlook screen is not sustainable.
This following script will give you the basic option to clear the team members and add all members referenced with their email addresses. It could be adapted easily to use another source (CSV, TXT, ...) as list of emails.
[string]$MyDistributionGroupToClean = "MyDistributionGroupName"
$AllEmailsToAdd ="email address removed for privacy reasons","email address removed for privacy reasons","email address removed for privacy reasons","email address removed for privacy reasons","email address removed for privacy reasons","email address removed for privacy reasons"
# -------------------------------------------------------
#Install-Module -Name ExchangeOnlineManagement -Force
Import-Module ExchangeOnlineManagement -DisableNameChecking
Connect-ExchangeOnline
# -------------------------------------------------------
$CurrentDLGroupMembers = Get-DistributionGroupMember -Identity $MyDistributionGroupToClean -ResultSize Unlimited
$CurrentDLGroupMembers #visual control
foreach($mymember in $CurrentDLGroupMembers)
{
$MemberEmail=$mymember.PrimarySMTPAddress
if($MemberEmail -eq "")
{
Write-Host " -> Member:", $mymember.Name, " - ", $mymember.PrimarySMTPAddress, "- NOT REMOVED - NO EMAIL" -ForegroundColor Red
}
else
{
Remove-DistributionGroupMember -Identity $MyDistributionGroupToClean -Member $mymember.PrimarySMTPAddress -Confirm:$false
Write-Host " -> Member:", $mymember.Name, " - ", $mymember.PrimarySMTPAddress, " - REMOVED" -ForegroundColor Green
}
}
$CurrentDLGroupMembers = Get-DistributionGroupMember -Identity $MyDistributionGroupToClean -ResultSize Unlimited
$CurrentDLGroupMembers #visual control
foreach($NewAccount in $AllEmailsToAdd)
{
Add-DistributionGroupMember -Identity $MyDistributionGroupToClean -Member $NewAccount -Confirm:$false
Write-Host " -> Member:", $NewAccount, " - ADDED" -ForegroundColor Green
}
$CurrentDLGroupMembers = Get-DistributionGroupMember -Identity $MyDistributionGroupToClean -ResultSize Unlimited
$CurrentDLGroupMembers #visual control
This solution is simple to use or delegate to a support team.
Fabrice Romelard
Sources Used:
- https://www.powershellgallery.com/packages/ExchangeOnlineManagement/3.0.0
- https://learn.microsoft.com/en-us/powershell/module/exchange/add-distributiongroupmember?view=exchange-ps
- https://learn.microsoft.com/en-us/powershell/module/exchange/update-distributiongroupmember?view=exchange-ps
- https://o365reports.com/2019/05/23/export-office-365-distribution-group-members-csv/
- https://m365scripts.com/exchange-online/how-to-add-bulk-users-to-distribution-group-in-office-365-via-powershell/
- https://techcommunity.microsoft.com/t5/office-365/trying-to-add-multiple-users-to-distribution-group-and-getting/m-p/910637
No RepliesBe the first to reply