The current article, is that first article on the five-article series, which is dedicated to the subject of managing Distribution Group in Office 365 and Exchange Online based environment using PowerShell.
The purpose of this article is to provide a “slim version” of PowerShell command index, that we need to use for common Distribution Group management tasks.
For each section in the article such as – Creating New Distribution Group and managing existing Distribution Groups, adding members to existing Distribution Group I have dedicated separated article.
If the “short version” of the PowerShell command is not sufficient for you, and you need more information or more examples, you are invited to read the specific article if the Distribution Group malmanagement via PowerShell article series.
Manage Distribution Group using PowerShell in Office 365 | Article Series
- Manage Distribution Group using PowerShell in Office 365 | PowerShell command reference | Part 1#5
- Manage Distribution Group using PowerShell in Office 365 | Creating New Distribution Group and managing existing Distribution Groups | Part 2#5
- Manage Distribution Group using PowerShell in Office 365 | Adding members to existing Distribution Group | Part 3#5
- Manage Distribution Group using PowerShell in Office 365 | view and export information about Distribution Group | Part 4#5
- Manage Distribution Group using PowerShell in Office 365 | Delete Distribution Group and members | Convert Distribution Group | Part 5#5
1. Creating New Distribution Group
Create NEW Distribution Group (use default settings).
PowerShell command syntax
1 | New-DistributionGroup -Name "<Distribution Group Name>" |
PowerShell command Example
1 | New-DistributionGroup -Name “Sales USA” |
Create NEW Distribution Group + set additional Distribution Group settings.
PowerShell command syntax
1 2 | New-DistributionGroup -Name <Distribution Group name> -DisplayName <DL display name> -Alias <Alias>-PrimarySmtpAddress <Email Address> -ManagedBy <identity> |
PowerShell command Example
1 2 | New-DistributionGroup -Name “Sales USA” -DisplayName “Sales USA mail list” -Alias “SalesUSA” -PrimarySmtpAddress SalesUSA@o365info.com -ManagedBy Brad |
Creating Distribution Groups by importing information from a CSV File
PowerShell command syntax
1 | Import-CSV <Path> | ForEach {New-DistributionGroup -Name $_.name -Type $_.Type} |
PowerShell command Example
1 | Import-CSV C:\Temp\DL-Group.csv | ForEach {New-DistributionGroup -Name $_.name -Type $_.Type} |
2. Manage existing Distribution Group settings
Enable or disable Distribution Group to get E-mail from external senders
To enable the external recipient to send E-mail to Exchange Online Distribution Group, we set the value of the parameter “RequireSenderAuthenticationEnabled” to $False.
PowerShell command syntax
1 | Set-DistributionGroup "<Distribution Group Name>" -RequireSenderAuthenticationEnabled $False |
PowerShell command Example
1 | Set-DistributionGroup “Sales USA” -RequireSenderAuthenticationEnabled $False |
Enable an external recipient to send E-mail to all Distribution Groups (bulk mode).
PowerShell command Example
1 | Get-DistributionGroup | Set-DistributionGroup -RequireSenderAuthenticationEnabled $False |
Adding or replacing Distribution Group owner
Replace existing Distribution Group owner
PowerShell command syntax
1 | Set-DistributionGroup -Identity "<Distribution Group Name>" –ManagedBy <Identity> |
PowerShell command Example
1 | Set-DistributionGroup -Identity "Sales USA" -ManagedBy Brad -BypassSecurityGroupManagerCheck |
Adding additional owner\s to Distribution Group
PowerShell command syntax
1 2 | Set-DistributionGroup “<Distribution Group name>” -ManagedBy @{Add='<Identity 1>','<Identity 2>'} |
PowerShell command Example
1 | Set-DistributionGroup “Sales USA” -ManagedBy @{Add='bob','brad'} |
Manage Distribution Group E-mail address
Set Distribution Group Primary E-mail address
PowerShell command syntax
1 | Set-DistributionGroup "<Distribution Group name>" -PrimarySmtpAddress <primary E-mail address> |
PowerShell command Example
1 | Set-DistributionGroup "Sales UK" -PrimarySmtpAddress SalesUK@o365info.com |
In our example, the CSV file name is Distribution-Groups-information.csv
Set Distribution Group Primary E-mail address by importing information from a CSV file
PowerShell command Example
1 2 3 4 5 6 7 8 | $GroupList = Import-CSV C:\temp\Distribution-Groups-information.csv ForEach ($group in $GroupList) { ForEach ($email in $GroupList) { } Set-DistributionGroup -BypassSecurityGroupManagerCheck -Identity $group.GroupName -PrimarySmtpAddress $email.email } |
Add additional E-mail address to existing Distribution Group E-mail address
PowerShell command Example
1 2 | Set-DistributionGroup “Sales UK” -emailaddresses @{Add=' |
Replace (remove) existing Distribution Group Alias E-mail
PowerShell command Example
1 |
Remove existing Distribution Group Alias E-mail addresses
PowerShell command syntax
1 | Set-DistributionGroup “<Distribution Group name>” -EmailAddresses @{Remove='<Identity 1>', '<Identity 2>' } |
PowerShell command Example
1 | Set-DistributionGroup "Sales UK" -EmailAddresses @{Remove=' [email protected]', ' [email protected]' } |
Hide Distribution Group from GAL (Global address list).
Hide Distribution Group from GAL (Global address list)
PowerShell command syntax
1 | Set-DistributionGroup "<Distribution Group Name>" -HiddenFromAddressListsEnabled $True |
PowerShell command Example
1 | Set-DistributionGroup "Sales UK" -HiddenFromAddressListsEnabled $True |
Set an existing Distribution Group to accept E-mail only from a specific sender
Set an existing Distribution Group to accept E-mail only from a specific sender
PowerShell command syntax
1 | Set-DistributionGroup "<Distribution Group Name>" –AcceptMessagesOnlyFrom <Allowed E-mail address 1>, < Allowed E-mail address 2> |
PowerShell command Example
1 | Set-DistributionGroup "Sales UK" –AcceptMessagesOnlyFrom Bradp@o365info.com,Angelina@o365info.com |
Define a Distribution Group Moderator
Define a Distribution Group Moderator
PowerShell command syntax
1 | Set-DistributionGroup "<Distribution Group Name>" –ModeratedBy <E-mail address>, <E-mail address> |
PowerShell command Example
1 | Set-DistributionGroup "Sales UK" –ModeratedBy Bradp@o365info.com,Angelina@o365info.com |
Send out of office reply for a Distribution Group
Send out of office reply for a Distribution Group
PowerShell command syntax
1 | Set-DistributionGroup "<Distribution Group Name>" –<strong>SendOofMessageToOriginatorEnabled $True </strong> |
PowerShell command Example
1 | Set-DistributionGroup "Sales UK" –<strong>SendOofMessageToOriginatorEnabled $True </strong> |
Assign “Send As” Permissions to Distribution Group
Assign “Send As” Permissions to Distribution Group
PowerShell command syntax
1 | Add-RecipientPermission "<Distribution Group Name>" -Trustee <Identity> -AccessRights SendAs -Confirm:$False |
PowerShell command Example
1 | Add-RecipientPermission "Sales UK" -Trustee Brad -AccessRights SendAs -Confirm:$False |
3. Adding users to a Distribution Group
Add a user (recipient) to a Distribution Group
Add a user (recipient) to a Distribution Group
PowerShell command syntax
1 | Add-DistributionGroupMember "<Distribution Group Name>" -Member <Identity> |
PowerShell command Example
1 | Add-DistributionGroupMember -Identity "Sales UK" -Member Bradp |
Add a list of users to a specific Distribution Group
Import Distribution Group members from a CSV File
PowerShell command Example
1 2 3 4 5 | $Userslist = Import-CSV C:\Temp\Distribution-Groups-Members.csv ForEach ($User in $Userslist) { Add-DistributionGroupMember -Identity "Sales France" -Member $User.PrimarySmtpAddress } |
Add user (recipient) to multiple distribution groups
PowerShell command syntax
1 2 3 | $Variable = "<Distribution Group name>","<Distribution Group name>","<Distribution Group name>" ForEach ($item in $Variable) {Add-DistributionGroupMember -Identity $item –Member <Identity>} |
Import Distribution Group members from a CSV File
PowerShell command Example
1 2 3 4 5 | $Userslist = Import-CSV C:\Temp\Distribution-Group-Users.csv ForEach ($User in $Userslist) { Add-DistributionGroupMember -Identity "Sales France" -Member $User.PrimarySmtpAddress } |
Add to Distribution Group all the users whom their department is Sales*
PowerShell command Example
1 2 3 4 5 | $SalesUsers = Get-User | Where {$_.Department -like “Sales*”} foreach ($User in $SalesUsers) { Add-DistributionGroupMember -Identity "<em>Sales </em>worldwide " -Member $User.name } |
Add user to distribution groups that were created in the last 48 hours
PowerShell command Example
1 2 3 4 5 | $AllNewDistributionGroups = Get-DistributionGroup | Where {$_.WhenCreated –ge ((Get-Date).AddHours(-48))} ForEach ($Group in $AllNewDistributionGroups) { Add-DistributionGroupMember -Identity $Group.name –Member Bradp } |
4. View information about Distribution Groups
In case that you want to get more detailed information about – Distribution Group management using PowerShell, and the subject of “view and export information about Distribution Groups”, you can read the following article – Manage Distribution Group using PowerShell in Office 365 | view and export information about Distribution Group | Part 4#5
Display all Distribution Groups list + details
PowerShell command Example
1 | Get-DistributionGroup |
Display Distribution Group Members
Display Distribution Group Members
PowerShell command syntax
1 | Get-DistributionGroupMember "<Distribution Group Name>" |
PowerShell command Example
1 | Get-DistributionGroupMember "Sales France" |
Count the number of Distribution Group members
PowerShell command Example
1 | (Get-DistributionGroupMember "IT").Count |
Display list of Distribution Groups with specific Email Domain name suffix
Display list of Distribution Groups with specific Email Domain
PowerShell command syntax
1 | Get-DistributionGroup | Where {$_.emailaddresses –like <"*Domain Name*">} | FT -Property Name,Alias,EmailAddresses -Autosize |
In our specific example, we look for Distribution Group that their E-mail address includes the domain name – o365info.com
PowerShell command Example
1 | Get-DistributionGroup | Where {$_.emailaddresses –like "*o365info.com*"} | FT -Property Name,Alias,EmailAddresses -Autosize |
Display information about Distribution Group that was updated before or after specific date range.
Get a list of Distribution Groups, that was created in the last 2 weeks
PowerShell command Example
1 | Get-DistributionGroup | Where {$_.WhenCreated –ge ((Get-Date).Adddays(-14))} | FT DisplayName,WhenCreated |
Display all Distribution Groups which their owner (managed by) is user X
PowerShell command Example
1 | Get-DistributionGroup | Where {$_.ManagedBy -like “*adele*”} | FT DisplayName,ManagedBy |
Display all Distribution Groups which have a moderator
PowerShell command Example
1 | Get-DistributionGroup | Where {$_.ModeratedBy -notlike “$null”} | FT DisplayName,ManagedBy |
Display all Distribution Groups that are synchronized from On-Premise Active Directory
PowerShell command Example
1 | Get-DistributionGroup | Where {$_.IsDirSynced -eq $true} | FT DisplayName, IsDirSynced |
Display Distribution Group information about delivery management
Display Distribution Groups which their delivery management allows external recipients
to send E-mail
Display Distribution Groups that accept E-mail from external recipients
PowerShell command Example
1 | Get-DistributionGroup | Where {$_.RequireSenderAuthenticationEnabled -eq $True} | FT DisplayName,RequireSenderAuthenticationEnabled |
Display Distribution Groups which their delivery management doesn’t allow external recipients to send E-mail
Display Distribution Groups that doesn’t accept E-mail from external recipients
PowerShell command Example
1 | Get-DistributionGroup | Where {$_.RequireSenderAuthenticationEnabled -eq $False } | FT DisplayName,RequireSenderAuthenticationEnabled |
Get information about Distribution Group membership of a specific user.
PowerShell command Example
1 2 3 4 5 6 7 8 | $User = read-host “User Name" $UserDName = (Get-Mailbox $User).name "The User " + $User + " is a member of the following Distribution Groups:" ForEach ($DistributionGroup in Get-Distributiongroup -resultsize unlimited) { if ((Get-Distributiongroupmember $DistributionGroup.identity | select -expand name) -contains $UserDName) {$DistributionGroup.name} } |
5. Removing members from Distribution Group
Delete (Remove) a Distribution Group
Delete (Remove) a Distribution Group
To delete an existing Distribution Group, we use the following PowerShell command:
PowerShell command syntax
1 | Remove-DistributionGroup "<Distribution Group Name>" |
PowerShell command Example
1 | Remove-DistributionGroup "Sales USA " |
Remove a member from a Distribution Group
PowerShell command syntax
1 | Remove-DistributionGroupMember -Identity "<Distribution Group name>" -Member "<Member name>" |
PowerShell command Example
1 | Remove-DistributionGroupMember -Identity "Sales USA" -Member "Bob" |
Remove user from all the Distribution Groups which he is a member in
PowerShell command Example
1 2 3 4 5 6 7 8 9 10 11 12 | $DistributionGroups = Get-Distributiongroup -resultsize unlimited $UserDName = read-host “Enter User Name" $UserDName = (Get-Mailbox $User).name "Searching which groups " + $User + " is a member of and removing membership..." ForEach ($Group in $DistributionGroups) { if ((Get-Distributiongroupmember $Group.Name | select -expand name) -contains $UserDName) { write-host "Removing user from group '$Group'" Remove-DistributionGroupMember -Identity "$Group" -Member "$UserDName" -Confirm:$false } } |
Remove all members from a Distribution Group
PowerShell command Example
1 2 3 4 5 | $DistributionGroupMember = Get-DistributionGroupMember "IT" ForEach ($member in $DistributionGroupMember) { Remove-DistributionGroupMember -Identity IT –Member $member.name -Confirm:$false } |
Additional Distribution Group malmanagement tasks | tips and tricks
How to convert Distribution Group into a security group
Copy members from Distribution Group to a security group
PowerShell command Example
1 2 3 4 5 | $Members = Get-DistributionGroupMember -id "<Name of the source group>" ForEach ($Member in $Members) { Add-DistributionGroupMember -Identity "<Name of the destination security group>" -Member $Member.name } |
Assign “Full Access” permissions to Distribution Group + use AutoMapping option
Extract Distribution Group member’s, and assign Full access permissions for each group member
PowerShell command Example
1 2 3 4 5 | $DistributionGroupName = Get-DistributionGroupMember "Sales France" ForEach ($Member in $DistributionGroupName) { Add-MailboxPermission -Identity "Bradp" -User $Member.name -AccessRights ‘FullAccess’ -InheritanceType all } |
Additional reading
- Set-DistributionGroup
- Get-DistributionGroup
- Get-DistributionGroupMember
- Add-DistributionGroupMember
- View members of a dynamic distribution group
- Automated replies and Distribution Groups
- Remove-DistributionGroupMember
Scripts
- How to export the distribution groups from Office 365 Exchange Online
- List all Distribution Groups and their Membership in Office 365
Video
The former article in the current article series
It is important for us to know your opinion on this article


I have a distribution group that I created but did not set it as a security group. Now I need to set it as a security group. What is the powershell command to change this distribution group and add the option of being a security group?
I have not found the line item needed to add this option.
Set-DistributionGroup -Name “HDGroup”
But this is as far as I have gotton and not sure what the syntax is for changing the distribution group to include the security switch which is only available when you create the distribution list in the gui.
As far as I know, it is not possible to change a distributiongroup from one type to another. You may need to redo the process. At least in the GUI it has to be done that way.
This script works for Office 365 and removes the single user from all Distribution Groups. It does go through all groups and fails on the ones where the user is not a member, but it’s quick and dirty.
$email= read-host -prompt “Email Address”
$DGs= Get-DistributionGroup
foreach( $dg in $DGs){
Remove-DistributionGroupMember $dg.name -Member $email -confirm:$false
}
get-distributiongrupmember not recognised
Looking for something that can list distrobution groups a user is a member of…
Kinda like this:
$User = read-host -Prompt “Email Address”
$user_dn = (Get-MsolUser -userprincipalname $user)
“User ” + $User + ” is a member of the following groups:”
foreach ($group in Get-Msolgroup -MaxResults 10000){ if ((Get-MsolGroupMember $group.identity | select -expand distinguishedname) -contains $user_dn){$group.name} }
but cannot make it work
Hello,
Need some ideas to automate adding or removing members in outlook with Shell scripting. Can i get the script please.
Thanks in advance.
Hello,
Need some ideas to automate adding or removing members in distribution group in outlook. Help me with the script.
Thanks
Bill
Very Good, thanks, do you know how to use the -and statement.
EG:
get-distributiongroup |where {$_.OrganizationalUnit -like “*namehere*” -and $_.OrganizationalUnit -like “*othernamehere*”}
as it does not work. I need to identify groups from certain org unit names
Great article !
Is there a power shell command that will hide or deny the sending to all Distribution Groups from 1 Distribution group?
In other words, I have 1 Distribution group, that I do not want to be able to send to any other Distribution groups, nor do I want them to see Distribution Groups within the GAL.
Thanks
Hello there! I simply would like to offer you a big tgumbs up for the great information
you’ve got right here on this post. I’ll be coming back
to your site for more soon.
Please help with this command. We want all users that have titles of Manager under the Province of ON and the City of (Toronto and Ottawa) will receive the Emails when we send emails to Dynamic DL HomeManagers. PLease see command below.
Command:
New-DynamicDistributionGroup -Name “HomeManagers” -OrganizationalUnit “domain.ca/Groups”-RecipientFilter {(RecipientType -eq ‘UserMailbox’) -and (Title -eq ‘Manager*’) -and (StateOrProvince -eq ‘ON’) -and (City -eq ‘Toronto’) -or (City -eq ‘Ottawa’)}
The command works but only Toronto users get the Emails but not the Ottawa users. Please advise.
Is there a script to get a list of all Exchange Distribution groups and whether or not they have the box checked for (Require that all senders are authenticated)? I am looking to audit this each year and looking to pull a quick list.
Hi. I’m going to create several (about 40) new distribution lists, but I would like to add owner to it also. Do I have to run different cmdlets to do this, or can I run one? How can I create 40 DL in Powershell in the easiest way? I’m a beginner at Powershell, so I’d really appreciate some tips here
Thanks.
Wonderful site. Lots of useful info here. I’m sending it to several pals ans additionally sharing in delicious. And obviously, thanks to your sweat!
Hi there, just wanted to tell you, I loved this post. It was inspiring.
Keep on posting!
Thanks-a-mundo for the post.Thanks Again.
I am looking for a script that can add distro lists to O365. But here is the caveat.
I have a .csv file. It lists DisplayName, GroupAlias, and SMTP Email Address. So it lists the group name, the display name of the user, and email address of the user. Some of the email addresses are for outside contacts. I need a script that can import the groups, import the users, and create the external contacts as well. Any help is appreciated. I know in advance i am asking for the Holy Grail of scripts, but I have a LOT of groups and users to add.
This is very helpfull
thank you very very very very much!
You are the best. Thanks for the fantastic job.
This blog was… how do I say it? Relevant!! Finally I’ve found something that helped me.
Cheers!
Do you know if a way to remove users from a distribution group that are NOT listed in the imported CSV?
So that way I could have a single CSV file, and a powershell script that adds users from the CSV to the distribution group, but then removes any users who are NOT in the csv file, ensuring the csv & membership is always matched.
DL created and added users to this DL. now i want to hide those users from DL.How can i hide, please suggest. Output should be as “if we double click DL it should not show the members in the DL.
I am looking for a script to grant the “-AcceptMessagesOnlyFrom” attribute to a user on the target DL and all nested DLs. Anyone have a good solution for this?
Hi, How to add an authenticated user for the existing distribution list. ( He/she must be able to send messages to the list).