#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False
#Add User to the Distribution Group
Add-DistributionGroupMember –Identity "[email protected]" -Member "[email protected]"
$GroupEmailID = "[email protected]"
$CSVFile = "C:\Temp\DL-Members.txt"
#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False
#Get Existing Members of the Distribution List
$DLMembers = Get-DistributionGroupMember -Identity $GroupEmailID -ResultSize Unlimited | Select -Expand PrimarySmtpAddress
#Import Distribution List Members from CSV
Import-CSV $CSVFile -Header "UPN" | ForEach {
#Check if the Distribution List contains the particular user
If ($DLMembers -contains $_.UPN)
{
Write-host -f Yellow "User is already member of the Distribution List:"$_.UPN
}
Else
{
Add-DistributionGroupMember –Identity $GroupEmailID -Member $_.UPN
Write-host -f Green "Added User to Distribution List:"$_.UPN
}
}
Here is what my text file looks like – One Email in each line:
$CSVFile = "C:\Temp\DL-Group-Members.csv"
Try {
#Connect to Exchange Online
Connect-ExchangeOnline -ShowBanner:$False
#Get date from CSV File
$CSVData = Import-Csv -Path $CSVFile
#Iterate through each row in the CSV
ForEach($Row in $CSVData)
{
#Get the Distribution Group
$Group = Get-DistributionGroup -Identity $Row.GroupEmail
If($Group -ne $Null)
{
#Get Exisiting Members of the Group
$GroupMembers = Get-DistributionGroupMember -Identity $Row.GroupEmail -ResultSize Unlimited | Select -Expand PrimarySmtpAddress
#Get Users to Add to the Group
$UsersToAdd = $Row.Users -split ","
#Add Each user to the Security group
ForEach ($User in $UsersToAdd)
{
#Check if the group has the member already
If($GroupMembers -contains $User)
{
Write-host "'$($User)' is already a Member of the Group '$($Group.DisplayName)'" -f Yellow
}
Else
{
Add-DistributionGroupMember –Identity $Row.GroupEmail -Member $User
Write-host -f Green "Added Member '$User' to the Group '$($Group.DisplayName)'"
}
}
}
Else
{
Write-host "Could not Find Group:"$Row.GroupName
}
}
}
Catch {
write-host -f Red "Error:" $_.Exception.Message
}
Prepare a CSV file containing the email addresses of the users you want to add to the distribution list. The CSV file should have a header row with the following column names: GroupEmail, and Users. Here is my CSV template: