Scenario
You have a new Executive or top level user that needs to be able to send emails to all distribution lists in the M365 Org, INCLUDING Restricted ones where only certain senders can communicate with the DL or Group. Not all groups are restricted and you need to only add them to the restricted groups and DLs. To know what groups have a restriction, you look for the AcceptMessagesOnlyFromSendersorMembers parameter when you get the DL properties.
I have created the following PowerShell commands that will allow you to accomplish this task successfully.
After connecting to Exchange Online PowerShell, review what DL and Dynamic DLs are restricted:
1 | Get-DistributionGroup | ? {$_.AcceptMessagesOnlyFromSendersOrMembers -ne $null} | Select-Object Identity |
Do the same for Dynamic DLs:
1 | Get-DynamicDistributionGroup | ? {$_.AcceptMessagesOnlyFromSendersOrMembers -ne $null} | Select-Object Identity |
Once those DLs are verified, we want to get the UPN of the user we want to add to the list. For this case we can use mynewuser01@ldlnet.net for this purpose.
In PowerShell, we want to pipe: ‘|’ multiple commands together so that we can accomplish the task with the list we just generated. We want to user the where ‘?’ parameter to filter the groups by the attribute, and then user the foreach ‘%’ statement so that we can add the user to each DL in the list. We are also going to use the array parameter ‘@{function=”data1″,”data2″,etc…}’ so that we can add multiple users if necessary for this function of add to add the user(s). If you wanted to remove the users, then change the add function to remove in the statement.
Here is the cmdlet:
1 | Get-DistributionGroup | ? {$_.AcceptMessagesOnlyFromSendersOrMembers -ne $null} | Select-Object Identity | % {Set-DistributionGroup $_.Identity -AcceptMessagesOnlyFromSendersOrMembers @{add="mynewuser01@ldlnet.net"}} |
Do the same for Dynamic DLs:
1 | Get-DynamicDistributionGroup | ? {$_.AcceptMessagesOnlyFromSendersOrMembers -ne $null} | Select-Object Identity | % {Set-DynamicDistributionGroup $_.Identity -AcceptMessagesOnlyFromSendersOrMembers @{add="mynewuser01@ldlnet.net"}} |
Conclusion
Exchange Online PowerShell provides many good tools to filter and complete tasks that might seem arduous when manually done in the GUI portal.
THANKS FOR READING!
REFERENCES:
How to add/remove allow sender of distribution group with – Microsoft Community
About Lance Lingerfelt
Lance Lingerfelt is an M365 Specialist and Evangelist with over 20 years of experience in the Information Technology field. Having worked in enterprise environments to small businesses, he is able to adapt and provide the best IT Training and Consultation possible. With a focus on AI, the M365 Stack, and Healthcare, he continues to give back to the community with training, public speaking events, and this blog.