Set Office 365 Distribution Group Delivery Restrictions via PowerShell

Setting delivery restrictions on exchange online distribution groups is quite a common task.Before proceed, Connect Exchange Online Remote PowerShell.

We can use the powershell cmdlet Set-DistributionGroup to configure delivery restriction with the parameter -AcceptMessagesOnlyFrom. It’s easy to create a powershell command to add multiple office 365 users to the -AcceptMessagesOnlyFrom attribute on the DL object but when doing this you’ll find that only the last one in the list has been added. This is because the attribute is an array. You can view this using the following command.

Get-DistributionGroup -Identity "<group-name<" | Select -expand AcceptMessagesOnlyFrom | FT Name

To add a new office 365 user to this list you have to get the already existing list and then add the new user to this list and set this new list to the attribute -AcceptMessagesOnlyFrom.

$lst = (Get-DistributionGroup "<group-name>").AcceptMessagesOnlyFrom 

$lst.Add("<user-name>")

Set-DistributionGroup "<group-name>" -AcceptMessagesOnlyFrom($lst)

You can also remove an user by removing user from the attribute -AcceptMessagesOnlyFrom.

$lst = (Get-DistributionGroup "<group-name>").AcceptMessagesOnlyFrom 

$lst.Remove("<user-name>")

Set-DistributionGroup "<group-name>" -AcceptMessagesOnlyFrom($lst)

Like wise, you can also add multiple office 365 users by importing users from text file. First create the text file Users.txt which includes one user name in each line

$UserList = Get-Content "C:\Users.txt"

$lst = (Get-DistributionGroup "<group-name>").AcceptMessagesOnlyFrom 

ForEach ($user in $UserList)
{
  $lst.Add($user)
}

Set-DistributionGroup "<group-name>" -AcceptMessagesOnlyFrom($lst)
Advertisement

Leave a Comment