Skip to content

Manage Distribution Groups by using PowerShell | Office 365

The current article is the first article of 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, managing existing Distribution Groups, and adding members to existing Distribution Group, I have dedicated separate articles.

Manage Distribution Group using PowerShell in Office 365 | Article Series

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 on Distribution Group management via PowerShell article series.

The article series includes the following articles:

  1. Manage Distribution Groups by using PowerShell | Office 365 (this article)
  2. Manage Distribution Group using PowerShell in Office 365 | Creating and managing Distribution Groups
  3. Manage Distribution Group using PowerShell in Office 365 | Adding members to existing Distribution Group
  4. Manage Distribution Group using PowerShell in Office 365 | view and export information about Distribution Group
  5. Manage Distribution Group using PowerShell in Office 365 | Delete Distribution Group and members | Convert Distribution Group

1. Creating New Distribution Group

In case you want more detailed information about Distribution Group management using PowerShell and the subject of “Creating Distribution Groups”, you can read the article Manage Distribution Group using PowerShell in Office 365 | Creating and managing Distribution Groups.

Create NEW Distribution Group (use default settings)

PowerShell command syntax:

New-DistributionGroup -Name "<Distribution Group Name>"

PowerShell command example:

New-DistributionGroup -Name "Sales USA"

Create NEW Distribution Group + set additional Distribution Group settings

PowerShell command syntax:

New-DistributionGroup -Name <Distribution Group name> -DisplayName <DL display name> -Alias <Alias> -PrimarySmtpAddress <Email Address> -ManagedBy <identity>

PowerShell command example:

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 CSV File

PowerShell command syntax:

Import-CSV <Path> | ForEach {New-DistributionGroup -Name $_.name -Type $_.Type}

PowerShell command example:

Import-CSV "C:\Temp\DL-Group.csv" | ForEach {New-DistributionGroup -Name $_.name -Type $_.Type}
Creating Distribution Groups by importing information from a CSV File

You can download the CSV files that we use in the article for demonstration purposes.

2. Manage existing Distribution Group settings

In case you want to get more detailed information about Distribution Group management using PowerShell and the subject of “Managing Distribution Groups”, you can read the article Manage Distribution Group using PowerShell in Office 365 | Creating and managing Distribution Groups.

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:

Set-DistributionGroup "<Distribution Group Name>" -RequireSenderAuthenticationEnabled $False

PowerShell command example:

Set-DistributionGroup "Sales USA" -RequireSenderAuthenticationEnabled $False

Enable external recipient to send E-mail to all Distribution Groups (bulk mode)

PowerShell command example:

Get-DistributionGroup | Set-DistributionGroup -RequireSenderAuthenticationEnabled $False

Replace existing Distribution Group owner

PowerShell command syntax:

Set-DistributionGroup -Identity "<Distribution Group Name>" -ManagedBy <Identity>

PowerShell command example:

Set-DistributionGroup -Identity "Sales USA" -ManagedBy "Brad" -BypassSecurityGroupManagerCheck

Adding additional owners to Distribution Group

PowerShell command syntax:

Set-DistributionGroup "<Distribution Group name>" -ManagedBy @{Add='<Identity 1>','<Identity 2>'}

PowerShell command example:

Set-DistributionGroup "Sales USA" -ManagedBy @{Add='bob','brad'}

Set Distribution Group Primary E-mail address

PowerShell command syntax:

Set-DistributionGroup "<Distribution Group name>" -PrimarySmtpAddress <primary E-mail address>

PowerShell command example:

Set-DistributionGroup "Sales UK" -PrimarySmtpAddress "SalesUK@o365info.com"

Set Distribution Group Primary E-mail address by importing information from CSV file

In our example, the CSV file name is Distribution-Groups-information.csv.

Set Distribution Group Primary E-mail address by importing information from CSV file

PowerShell command example:

$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
}

You can download the CSV files that we use in the article for demonstration purposes.

Add additional E-mail address to existing Distribution Group E-mail address

PowerShell command example:

Set-DistributionGroup "Sales UK" -emailaddresses @{Add='SalesUK02@o365info.com','SalesUK02@o365info.com','SalesUK02@o365info.com'}

Replace (remove) existing Distribution Group Alias E-mail

PowerShell command example:

Set-DistributionGroup "Sales UK" -emailaddresses 'SalesUK02@o365info.com','SalesUK03@o365info.com'

Remove existing Distribution Group Alias E-mail addresses

PowerShell command syntax:

Set-DistributionGroup "<Distribution Group name>" -EmailAddresses @{Remove='<Identity 1>','<Identity 2>'}

PowerShell command example:

Set-DistributionGroup "Sales UK" -EmailAddresses @{Remove=' Sales01@o365info.com','Sales02@o365info.com'}

Hide Distribution Group from GAL (Global Address List)

PowerShell command syntax:

Set-DistributionGroup "<Distribution Group Name>" -HiddenFromAddressListsEnabled $True

PowerShell command example:

Set-DistributionGroup "Sales UK" -HiddenFromAddressListsEnabled $True

Set an existing Distribution Group to accept E-mail only from specific sender

PowerShell command syntax:

Set-DistributionGroup "<Distribution Group Name>" –AcceptMessagesOnlyFrom <Allowed E-mail address 1>, < Allowed E-mail address 2>

PowerShell command example:

Set-DistributionGroup "Sales UK" –AcceptMessagesOnlyFrom Bradp@o365info.com,Angelina@o365info.com

Define Distribution Group Moderator

PowerShell command syntax:

Set-DistributionGroup "<Distribution Group Name>" -ModeratedBy <E-mail address>, <E-mail address>

PowerShell command example:

Set-DistributionGroup "Sales UK" -ModeratedBy Bradp@o365info.com,Angelina@o365info.com

Send Out of Office reply for Distribution Group

PowerShell command syntax:

Set-DistributionGroup "<Distribution Group Name>" -SendOofMessageToOriginatorEnabled $True

PowerShell command example:

Set-DistributionGroup "Sales UK" –SendOofMessageToOriginatorEnabled $True

Assign “Send As” Permissions to Distribution Group

PowerShell command syntax:

Add-RecipientPermission "<Distribution Group Name>" -Trustee <Identity> -AccessRights SendAs -Confirm:$False

PowerShell command example:

Add-RecipientPermission "Sales UK" -Trustee Brad -AccessRights SendAs -Confirm:$False

3. Adding users to Distribution Group

In case you want to get more detailed information about Distribution Group management using PowerShell and the subject of “Adding members to existing Distribution Group”, you can read the article Manage Distribution Group using PowerShell in Office 365 | Adding members to existing Distribution Group.

Add user (recipient) to Distribution Group

PowerShell command syntax:

Add-DistributionGroupMember "<Distribution Group Name>" -Member "<Identity>"

PowerShell command example:

Add-DistributionGroupMember -Identity "Sales UK" -Member "Bradp"

Import Distribution Group members from CSV File

Using a CSV file as a source of information for Distribution Group members -03

PowerShell command example:

$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:

$Variable = "<Distribution Group name>", "<Distribution Group name>", "<Distribution Group name>"
ForEach ($item in $Variable) { 
    Add-DistributionGroupMember -Identity $item –Member <Identity> 
}

Add to Distribution Group all users whose department is Sales*

PowerShell command example:

$SalesUsers = Get-User | Where { $_.Department -like "Sales*" }
foreach ($User in $SalesUsers) {
    Add-DistributionGroupMember -Identity "Sales worldwide" -Member $User.name
}

You can download the CSV files that we use in the article for demonstration purposes.

Add user to distribution groups that were created in last 48 hours

PowerShell command example:

$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 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 article Manage Distribution Group using PowerShell in Office 365 | view and export information about Distribution Group.

Display all Distribution Groups list + details

Get-DistributionGroup

Display Distribution Group Members

PowerShell command syntax:

Get-DistributionGroupMember "<Distribution Group Name>"

PowerShell command example:

Get-DistributionGroupMember "Sales France"

Count number of Distribution Group members

PowerShell command example:

(Get-DistributionGroupMember "IT").Count

Display list of Distribution Groups with specific Email Domain

PowerShell command syntax:

Get-DistributionGroup | Where {$_.emailaddresses –like <"*Domain Name*">} | FT -Property Name,Alias,EmailAddresses -Autosize

In our specific example, we look for Distribution Groups whose E-mail address includes the domain name o365info.com.

PowerShell command example:

Get-DistributionGroup | Where {$_.emailaddresses –like "*o365info.com*"} | FT -Property Name,Alias,EmailAddresses -Autosize

Get list of Distribution Groups that were created in last 2 weeks

Display information about Distribution Group that was updated before or after specific date range.

PowerShell command example:

Get-DistributionGroup | Where {$_.WhenCreated –ge ((Get-Date).Adddays(-14))} | FT DisplayName,WhenCreated

Display all Distribution Groups whose owner (managed by) is user X

PowerShell command example:

Get-DistributionGroup | Where {$_.ManagedBy -like "*adele*"} | FT DisplayName,ManagedBy

Display all Distribution Groups which have a moderator

PowerShell command example:

Get-DistributionGroup | Where {$_.ModeratedBy -notlike "$null"} | FT DisplayName,ManagedBy

Display all Distribution Groups synchronized from On-Premise Active Directory

PowerShell command example:

Get-DistributionGroup | Where {$_.IsDirSynced -eq $true} | FT DisplayName, IsDirSynced

Display Distribution Groups that accept E-mail from external recipients

PowerShell command example:

Get-DistributionGroup | Where {$_.RequireSenderAuthenticationEnabled -eq $True} | FT DisplayName,RequireSenderAuthenticationEnabled

Display Distribution Groups that don’t accept E-mail from external recipients

PowerShell command example:

Get-DistributionGroup | Where {$_.RequireSenderAuthenticationEnabled -eq $False } | FT DisplayName,RequireSenderAuthenticationEnabled

Get information about Distribution Group membership of specific user

PowerShell command example:

$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

In case you want to get more detailed information about Distribution Group management using PowerShell and the subject of “Delete Distribution Group and members, Convert Distribution Group type and more”, you can read the article Manage Distribution Group using PowerShell in Office 365 | Delete Distribution Group and members | Convert Distribution Group.

Delete (Remove) Distribution Group

To delete an existing Distribution Group, we use the following PowerShell command.

PowerShell command syntax:

Remove-DistributionGroup "<Distribution Group Name>"

PowerShell command example:

Remove-DistributionGroup "Sales USA"

Remove member from Distribution Group

PowerShell command syntax:

Remove-DistributionGroupMember -Identity "<Distribution Group name>" -Member "<Member name>"

PowerShell command example:

Remove-DistributionGroupMember -Identity "Sales USA" -Member "Bob"

Remove user from all Distribution Groups which he is a member of

PowerShell command example:

$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 Distribution Group

PowerShell command example:

$DistributionGroupMember = Get-DistributionGroupMember "IT"
ForEach ($member in $DistributionGroupMember) {
    Remove-DistributionGroupMember -Identity IT –Member $member.name -Confirm:$false
}

Additional Distribution Group management tasks | Tips and tricks

Copy members from Distribution Group to security group

PowerShell command example:

$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
}

Extract Distribution Group members and assign Full access permissions for each group member

PowerShell command example:

$DistributionGroupName = Get-DistributionGroupMember "Sales France"
ForEach ($Member in $DistributionGroupName) {
    Add-MailboxPermission -Identity "Bradp" -User $Member.name -AccessRights ‘FullAccess’ -InheritanceType all
}

That’s it!

All the PowerShell commands that were reviewed are wrapped in the Distribution list.ps1 PowerShell script.

o365info Team

o365info Team

This article was written by our team of experienced IT architects, consultants, and engineers.

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *