Exchange-Online-PowerShell-Recipes

Compare Antispam Policies

Problem

You want to compare the differences between two antispam policies.

Solution

A simple Compare-Object cmdlet will not give the results you might expect. Use this approach to comparing each property

Save The Policies to Variables

$policy1 = Get-HostedContentFilterPolicy -Identity "ENTER FIRST POLICY NAME HERE"
$policy2 = Get-HostedContentFilterPolicy -Identity "ENTER SECOND POLICY NAME HERE"

Save the Properties of the new policy objects to a variable called $props.

$props = $policy1 | 
Get-Member -MemberType Properties |
Select-Object -ExpandProperty Name

Compare Each Property and Show Only The Differences

$props | Foreach-Object {if($policy1.($_).ToString() -ne $policy2.($_).ToString()){Write-Host $_ ": " $policy1.($_) "<>" $policy2.($_)}}

Discussion

This is a quick way to show the differences between policies. The results are noisy. You will see obvious differences like Name, Guid, Created and Modified Dates, etc.