Skip to content

Script to Configure Trusted Domains for Junk Email Settings in Exchange Online for All Mailboxes

Exchange Online Trusted Domains Configuration Script

Section titled “Exchange Online Trusted Domains Configuration Script”

This PowerShell script automates the configuration of trusted domains for junk email settings across all user mailboxes in Exchange Online. By importing trusted domains from a CSV file and applying them systematically using Set-MailboxJunkEmailConfiguration, this script prevents legitimate emails from being marked as spam, significantly improving email deliverability for trusted business partners and domains.


FeatureCapabilityBusiness Value
Bulk Domain ImportImports trusted domains from CSV fileStreamlines domain management process
Hash Table OptimizationBuilds efficient hash table for domain storageImproves script performance and memory usage
Universal ApplicationApplies settings to all mailboxes in organizationEnsures consistent email filtering policies
Junk Email ConfigurationUpdates trusted senders and domains listReduces false positives in spam filtering
Exchange Online IntegrationUses native Exchange Online PowerShell cmdletsProvides reliable and supported configuration method

  • Exchange Online PowerShell module installed
  • Exchange Administrator or appropriate permissions
  • CSV file containing trusted domains list

Required structure:

  • Column name: domains
  • Format: One domain per row

Example CSV:

domains
trustedpartner1.com
trustedpartner2.com
importantclient.org

Terminal window
# Import trusted domains from CSV file
$domains = import-csv -path C:\Users\GXXXX\Downloads\xxx.csv
# Build hash table for efficient domain storage
$hash = @{}
$hash.Add = $domains | Select-Object -ExpandProperty domains
# Connect to Exchange Online
Connect-ExchangeOnline
# Retrieve all mailboxes in the organization
$mailboxes = Get-Mailbox
# Apply trusted domains to each mailbox
foreach ($mailbox in $mailboxes) {
Set-MailboxJunkEmailConfiguration -Identity $mailbox.Identity -TrustedSendersAndDomains $hash
}

  • Imports trusted domains from specified CSV file
  • Creates hash table for optimized performance
  • Validates domain format and structure
  • Establishes authenticated connection to Exchange Online
  • Retrieves comprehensive list of all user mailboxes
  • Prepares for bulk configuration operations
  • Iterates through each mailbox in the organization
  • Applies trusted domains configuration to junk email settings
  • Ensures consistent email filtering policies across all users

  • Reduces false positives for trusted business partners
  • Ensures important communications reach user inboxes
  • Minimizes business disruption from misclassified emails
  • Automates repetitive configuration across all mailboxes
  • Centralizes trusted domain management through CSV file
  • Provides consistent policy application organization-wide
  • Maintains spam protection while allowing trusted sources
  • Supports business relationships with reliable email delivery
  • Reduces user frustration from missing important emails

  • Regularly update trusted domains list as business relationships change
  • Validate domain entries before script execution
  • Maintain backup copies of trusted domain lists
  • Test in pilot environment before organization-wide deployment
  • Schedule during low-usage periods to minimize user impact
  • Monitor execution logs for any errors or issues
  • Use service accounts with appropriate Exchange Online permissions
  • Implement principle of least privilege for script execution
  • Document and review access requirements regularly

  • Quarterly audits of trusted domains list
  • Remove obsolete domains no longer needed
  • Add new trusted partners as relationships develop
  • Monitor script execution time in large organizations
  • Consider batch processing for very large mailbox counts
  • Optimize CSV file size by removing duplicate entries

Key Takeaway: This script provides an efficient, automated solution for managing trusted domains across your Exchange Online environment, ensuring reliable email delivery while maintaining robust spam protection.

Ideal for: Organizations with multiple trusted business partners, frequent inter-company communications, or those experiencing issues with legitimate emails being incorrectly classified as junk mail.

$domains = import-csv -path C:\Users\GXXXX\Downloads\xxx.csv $hash = @{} $hash.Add =  $domains | Select-Object -ExpandProperty domains

Connect-ExchangeOnline $mailboxes = Get-Mailbox foreach ($mailbox in $mailboxes) {     Set-MailboxJunkEmailConfiguration -Identity $mailbox.Identity -TrustedSendersAndDomains $hash }