← All scripts

Microsoft 365 & Entra ID

Add Users to Anti-Phishing Policy

Adds every mailbox user to the protected-users list of a Defender for Office 365 anti-phishing (impersonation) policy you select.

Download .ps120 lines · PowerShell

Replace generic placeholder values (tenant, domain, secrets) for your own environment before running. Read it first and test safely.

Connect-ExchangeOnline

Get-AntiPhishPolicy | Select-Object Identity

$policy = Read-Host "Copy and paste the name of the Anti-Phishing Policy you want to add users to. This will add all users that have a mailbox."

$Users = Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName, userPrincipalName

$TransformedUsers = $Users | ForEach-Object {
    [PSCustomObject]@{
        User = "$($_.DisplayName);$($_.userPrincipalName)"
    }
}

ForEach ($User in $TransformedUsers){

Set-AntiPhishPolicy -Identity $policy -TargetedUsersToProtect @{add=$User.User}

Write-Host -ForegroundColor Green $User.User "is added!"
}