Basically customer wanted to stop users from sending emails to any/all distribution list in the company. Use Case was to block user from sending mass emails before they leave the company. Whatever the case I had to come up with a script to do it to all over 150 DL. I wasn’t going to do it one by one from console so I made a cmdlet.
Below is the powershell script for Office 365. Once you are connected you can run the script.
Make a DL / Security Group called “Denied Users” and add users or user to that DL if you want to block him/her from sending emails to DL. Please note that this script will only add the Denied User DL in block list once i.e only the existing DLs will be modified. So if you create a new DL after running this script then that DL will not have Denied users in rejected user and hence you will have to re-run the script or you can run this script daily via windows scheduled tasks.
$temp = Get-DistributionGroup -ResultSize Unlimited
foreach($group in $temp)
{
Set-DistributionGroup -Identity $group -RejectMessagesFromDLMembers "Denied Users"
}
#Remove the above setting incase they change their mind.
#Set-DistributionGroup -Identity $group -RejectMessagesFromDLMembers $null