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

1 min. readlast update: 09.26.2024

This script imports a list of trusted domains from a CSV file, builds a hash table, and applies these trusted domains to all user mailboxes in Exchange Online. Using Set-MailboxJunkEmailConfiguration, the script updates each mailbox's junk email settings to include the specified trusted senders and domains. This helps prevent emails from trusted sources from being marked as spam or junk, improving email deliverability for specific domains.

Here is the script

$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
}

Was this article helpful?