We've been working a lot recently at work on the whitelists for Exchange Online. It'd be nice to have a script to back up all of those whitelists/blacklists for future reference.

The following requires you have a Connect-ExchangeOnline function, as well as PSCX installed (For write-zip)

You can throw a -SendEmail flag, which will use Send-MailMessage to send a mail with the created ZIP file to the defined addresses.

Function Backup-ExchangeWhiteLists {

    #region Varialbes
        $BackupPath = "C:\scripts\powershell\EOL_WhitelistBackups\"
            New-Item $BackupPath -ItemType Directory -ErrorAction SilentlyContinue -Force
        [String]$Runtime = Get-Date -Format
        $BackupZip = $BackupPath + $Runtime + ".zip"
        $StartingLocation = Get-Location

        $EmailRecipient = '[email protected]'
        $EmailSenders   = '[email protected]'
        $SMTPServer     = 'mysmtpserver.riston.local'

    $ConnectionState = (Get-PSSession | ? {$_.ConfigurationName -eq "Microsoft.Exchange" -and $_.State -eq "Opened" -and $_.ComputerName -eq ""}).count
        if ($ConnectionState -eq 0) {
            #return "Disconnected"

    $AllPolicies = Get-HostedContentFilterPolicy

    foreach ($SpamPolicy in $AllPolicies) {
        $OutFile = $BackupPath + $Runtime + "_" + $SpamPolicy.Name + "_AllowedSenderDomains.csv"
        $SpamPolicy.AllowedSenderDomains | Export-Csv -Path $OutFile -NoTypeInformation

        $OutFile = $BackupPath + $Runtime + "_" + $SpamPolicy.Name + "_AllowedSenders.csv"
        $SpamPolicy.AllowedSenders | Export-Csv -Path $OutFile -NoTypeInformation

        $OutFile = $BackupPath + $Runtime + "_" + $SpamPolicy.Name + "_BlockedSenderDomains.csv"
        $SpamPolicy.BlockedSenderDomains | Export-Csv -Path $OutFile -NoTypeInformation

        $OutFile = $BackupPath + $Runtime + "_" + $SpamPolicy.Name + "_BlockedSenders.csv"
        $SpamPolicy.BlockedSenders | Export-Csv -Path $OutFile -NoTypeInformation


    Set-Location $BackupPath

    $BackupFiles = Get-ChildItem $BackupPath*csv
    $BackupFiles | Write-Zip -Level 9 -OutputPath $BackupZip
    $BackupFiles | Remove-Item -Force -ErrorAction SilentlyContinue

    Set-Location $StartingLocation

    if ($SendEmail) {
        Send-MailMessage -SmtpServer $SMTPServer -To $EmailRecipient -From $EmailSenders -Subject "Exchange Spam Filter Backups - $($Runtime)" -Attachments $BackupZip


This results in a folder of .ZIP files with contents of all of the [Allowed\Denied] [Senders\Sender Domains] for your backups.

Until Next Time!


Popular Posts