How to add mail account to block sender O365

Today I will talk a little about O365 configuration, in this case I will explain how to add an email account to the black list in O365. All this task will be done by PowerShell, so let’s begin!

First of all, we need to connect to the O365:

$sesion=New-PSSession -ConfigurationName Microsoft.Exchange -connectionUri "" -Credential $cred -Authentication basic -Allowredirection
Import-PsSession $sesion

After typing all this parameters will be connected to the Exchange Online console, but if you want more information about how to connect, you can click on to get more info about the process

Since we are connected, we can start doing our work, in this case we are configuring the user with email, in the trustedsendersandomains we can configure the white list for this user, while the parameter blockedsendersanddomains are the black list for this user. Let’s give an example:

Set-MailboxJunkEmailConfiguration -Identity -TrustedSendersAndDomains, -BlockedSendersAndDomains,,

So, your next question will be, what If I want to check which domains and users are configured for a certain user? We can do it with the following:

Get-MailboxJunkEmailConfiguration -Identity

Easy, isn’t it? But I’m sure that you’re waiting for the next level of configuration, I want to configure the black list and white list for all the users at the same time, so let’s begin:

We’re using the same parameters as before, but take into account that we are retrieving all the mailboxes of the users and we are configuring the same parameters:

Get-Mailbox | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains, -BlockedSendersAndDomains,,

Finally, and as a bonus, we have the following parameter, with this, it is possible to remove the configuration applied for one user in particular (sorry I did no test with all the users in the domain)

Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains @{remove="","userspam@spamdomain"}

I hope this help some of you to understand how to configure the white list and black list in O365 with PowerShell. till the next time!


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s