Oct 12 2022 08:03 AM
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: