Bulk update e-mail whitelist (Set-MailboxJunkEmailConfiguration)

A request came in from a client to develop an easier way to populating the e-mail whitelist for all new starters, or anyone that wants a new list.

My colleague had tried exporting the current whitelist from a user with

Get-MailboxJunkEmailConfiguration "Surname, FirstName" | select-object @{name='WhiteList';Expression={[string]::Join(',', ($_.TrustedSendersAndDomains))}} | export-CSV c:\temp\whitelist.csv

This outputs the entries and comma separates, looks great but when attempting to import back in to another user it fails with an error about it being a string. You can’t export to a variable very well becuase it’s a Microsoft.Exchange.Data.MultiValuedProperty (you can use Get-Member to discover this). I believe my colleague had followed the article here in order to export the multivalued properties.

So the request came to me to “simply populate the trusted senders and domains either with a full overwrite from text file, or using += or @{add=’$list}”, easy hey? 🙂

Instead of trying to alter the data in any way when exporting I used a simple script to export to a text file, it lists the e-mail addresses and domains line by line.

Output script:

$file = New-Item -type file "C:\Temp\Test.txt" -Force
$user = "Surname, FirstName"
$dc = 'domaincontroller.domain.com'
$Mailbox = Get-MailboxJunkEmailConfiguration $user -domaincontroller $dc
$mailbox.TrustedSendersAndDomains | out-file $file

Turns out that get-content will automatically create an array from an input, so there was no need to export to a CSV or similar. Then I could just run a foreach to iterate through the entries. Now you can have a text file with all safe senders / domains list, or just the sender you wish to add, the script will then append to the list.

Input script:

$addresses = Get-Content c:\temp\test.txt 
$mailbox = '[email protected]'
$dc = 'domaincontroller.domain.com'
Foreach ($address in $addresses)
Set-MailboxJunkEmailConfiguration -Identity $mailbox -TrustedSendersAndDomains @{Add=$address} -domaincontroller $dc

Check out Get-Content on TechNet.

Posted in Guides Tagged with: , ,
One comment on “Bulk update e-mail whitelist (Set-MailboxJunkEmailConfiguration)
  1. Jimmy says:

    Thank goodness someone payed attention in PowerShell class!

Leave a Reply

Your email address will not be published. Required fields are marked *