Add Distribution Group Members
through PowerShell
As an Office 365 administrator, you can add members to the distribution list through windows PowerShell.
You will be required to use Add-DistributionGroupMember command in the Windows PowerShell.
First connect to exchange online by running the below command.
?
1Connect-ExchangeOnline
Adding Single Member to Distribution List
To add single member to the distribution group, run the following command in PowerShell.
?
1Add-DistributionGroupMember –Identity "testin@[Link]" -Member <a href="[Link]
Adding Multiple Distribution Group Members
using PowerShell
To add multiple members, you will need a .txt file containing members email address.
Create a .txt file as shown in the image below.
Once you have created the .txt file, run the below command in Windows PowerShell.
?
1$GroupEmailID = "testin@[Link]"
2
3$CSVFile = "C:\Users\dell\Desktop\[Link]"
4
5
6
7#Connect to Exchange Online
8
9Connect-ExchangeOnline -ShowBanner:$False
1
0
1
1#Get Existing Members of the Distribution List
1
2$DLMembers = Get-DistributionGroupMember -Identity $GroupEmailID -ResultSize Unlimited | Select -Expand PrimarySmtpAddress
1
3
1
4#Import Distribution List Members from CSV
1
5Import-CSV $CSVFile -Header "UPN" | ForEach {
1
6 #Check if the Distribution List contains the particular user
1
7 If ($DLMembers -contains $_.UPN)
1
8 {
1
9 Write-host -f Yellow "User is already member of the Distribution List:"$_.UPN
2
0 }
2
1 Else
2
2 {
2
3 Add-DistributionGroupMember –Identity $GroupEmailID -Member $_.UPN
2
4 Write-host -f Green "Added User to Distribution List:"$_.UPN
2
5 }
2
6}
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9
4
0
4
1
4
2
4
3
Add Members in Multiple Distribution Lists
Adding members to multiple distribution lists can be time taking as we need to create multiple .txt files. But
there is also a way to perform this operation through Windows PowerShell. Please follow the procedure
below.
Prepare a csv file containing group email address and users like as shown below.
Now run the below command to add members to multiple distribution groups.
?
1$CSVFile = "C:\Users\dell\Desktop\[Link]"
2
3Try {
4
5 #Connect to Exchange Online
6
7 Connect-ExchangeOnline -ShowBanner:$False
8
9
1
0 #Get date from CSV File
1
1 $CSVData = Import-Csv -Path $CSVFile
1
2
1
3 #Iterate through each row in the CSV
1
4 ForEach($Row in $CSVData)
1
5 {
1
6 #Get the Distribution Group
1
7 $Group = Get-DistributionGroup -Identity $[Link]
1
8
1
9 If($Group -ne $Null)
2
0 {
2
1 #Get Exisiting Members of the Group
2
2 $GroupMembers = Get-DistributionGroupMember -Identity $[Link] -ResultSize Unlimited | Select -Expand PrimarySmtpAddres
2
3
2
4 #Get Users to Add to the Group
2
5 $UsersToAdd = $[Link] -split ","
2
6
2
7 #Add Each user to the Security group
2
8 ForEach ($User in $UsersToAdd)
2
9 {
3
0 #Check if the group has the member already
3
1 If($GroupMembers -contains $User)
3
2 {
3
3 Write-host "'$($User)' is already a Member of the Group '$($[Link])'" -f Yellow
3
4 }
3
5 Else
3
6 {
3
7 Add-DistributionGroupMember –Identity $[Link] -Member $User
3
8 Write-host -f Green "Added Member '$User' to the Group '$($[Link])'"
3
9 }
4
0 }
4
1 }
4
2 Else
4
3 {
4
4 Write-host "Could not Find Group:"$[Link]
4
5 }
4
6 }
4
7}
4
8Catch {
4
9 write-host -f Red "Error:" $_.[Link]
5
0}
5
1
5
2
5
3
5
4
5
5
5
6
5
7
5
8
5
9
6
0
6
1
6
2
6
3
6
4
6
5
6
6
6
7
6
8
6
9
7
0
7
1
7
2
7
3
7
4
7
5
7
6
7
7
7
8
7
9
8
0
8
1
8
2
8
3
8
4
8
5
8
6
8
7
8
8
8
9
9
0
9
1