Forum Discussion
Limit the number domains in recipient list on mail
- Sep 20, 2023
Hi IanaMac,
yes, you need to add domains manually if you are using GUI, but you can add domains automatically to the list using a PowerShell script to read the list of domains from a file and then add them to the list of blocked domains in the transport rule or mail flow rule.Here is an example of a PowerShell script to add domains automatically to the list of blocked domains in a transport rule from the .txt. file:
# Import the Exchange module Import-Module Exchange # Get the transport rule $transportRule = Get-TransportRule "Block messages to external domains" # Add the domains to the list of blocked domains foreach ($domain in Get-Content "Domains.txt") { $transportRule.Conditions.RecipientDomain.BlockedDomains.Add($domain) } # Set the transport rule Set-TransportRule $transportRule
- To use this script, you would first need to create a text file called Domains.txt that contains the list of domains that you want to block.- Once you have created the text file, you can run the PowerShell script to add the domains to the list of blocked domains in the transport rule.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
thanks for this the transport and mailflow rules mean that we would have to add 100 of domains to the list...
I will look at the documentation now
Hi IanaMac,
yes, you need to add domains manually if you are using GUI, but you can add domains automatically to the list using a PowerShell script to read the list of domains from a file and then add them to the list of blocked domains in the transport rule or mail flow rule.
Here is an example of a PowerShell script to add domains automatically to the list of blocked domains in a transport rule from the .txt. file:
# Import the Exchange module
Import-Module Exchange
# Get the transport rule
$transportRule = Get-TransportRule "Block messages to external domains"
# Add the domains to the list of blocked domains
foreach ($domain in Get-Content "Domains.txt") {
$transportRule.Conditions.RecipientDomain.BlockedDomains.Add($domain)
}
# Set the transport rule
Set-TransportRule $transportRule
- To use this script, you would first need to create a text file called Domains.txt that contains the list of domains that you want to block.
- Once you have created the text file, you can run the PowerShell script to add the domains to the list of blocked domains in the transport rule.
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If the post was useful in other ways, please consider giving it Like.
Kindest regards,
Leon Pavesic
- IanaMacOct 02, 2023Brass Contributorthanks for the help!!!!