How to disable POP and IMAP for all mailboxes in Office 365

Disable POP and IMAP for all Office 365 users via PowerShell

First of all, why disable IMAP or POP? Two reasons:

  1. IMAP and POP are less secure mail protocols that are now around 30 years old.
  2. In our experience, IMAP and POP is seldom used for legitimate purposes. In almost every breach we’ve discovered, the attackers have used IMAP or POP protocols to download user mailboxes to another device.

If you’re looking for instances of unauthorised IMAP or POP access in your mailboxes, I recommend you also run this script. Typically hackers will log into user mailboxes from an unexpected location. This script runs each IP address used to login to your tenant against an IP locator API. It then exports a list of unique IP addresses, their location and the user agent of the device that was logging in. The IMAP entries will usually display in the list with a user agent of CBAInProd.

Hacker Logging On to Office 365 With CBAInPROD User Agent

In the above record from the Office 365 Unified Audit Log, an unauthorized user is accessing an account from an IP located in China.

Hacker Logging In From Unexpected Location

You can confirm this by searching the Unified Audit Log for ‘MailboxLogin’ Operations, then filtering by that IP address.

Hacker Using IMAP To Download Office 365 Email

In order to prevent attackers from downloading all of your user data in this way, you can disable IMAP and POP for all users via PowerShell.

There are two parts to this – you’ll want to disable it for all existing mailboxes as well as all future mailboxes.

If you already have a connection to Exchange Online via PowerShell, you can copy and paste the following cmdlets, otherwise you can use the scripts further down.

Disabling IMAP and POP for all future mailboxes

Get-CASMailboxPlan -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" } | set-CASMailboxPlan -ImapEnabled $false -PopEnabled $false

Disabling IMAP and POP for all existing mailboxes

Get-CASMailbox -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" } | Select-Object @{n = "Identity"; e = {$_.primarysmtpaddress}} | Set-CASMailbox -ImapEnabled $false -PopEnabled $false

The following scripts will take care of the whole operation for you. The first one will disable POP and IMAP for a single Office 365 tenant. The second script is for Microsoft Partners and will disable IMAP and POP for all mailboxes in all customer tenants. Note that these scripts do not work with MFA on the admin account.

How to run these scripts to disable IMAP and POP in Office 365 via PowerShell

  1. Double click on either of the scripts below to select it all
  2. Copy and paste it into Visual Studio Code and save it as a .ps1 file
  3. To allow these scripts to work with an MFA enabled account, you may need to whitelist your current static IP for MFA.
  4. Run it by pressing F5
  5. Enter the credentials of an Office 365 global admin, Exchange admin or delegated administrator
  6. Wait for the script to complete. If you’re running this across a number of tenants, you’ll probably be waiting a while.Disable IMAP and POP on All Customer Office 365 Tenants

How to disable IMAP and POP in a single Office 365 tenant via PowerShell

$credential = Get-Credential
$Session = New-PSSession -ConnectionUri https://outlook.office365.com/powershell-liveid/ `
    -ConfigurationName Microsoft.Exchange -Credential $credential `
    -Authentication Basic -AllowRedirection
Import-PSSession $Session -CommandName Get-CASMailbox, Set-CasMailbox, Get-Casmailboxplan, set-casmailboxplan, Get-OrganizationConfig
Write-Host "Attempting IMAP and POP operations on $((Get-OrganizationConfig).DisplayName)" -ForegroundColor Yellow
    
Write-Host "Disabling IMAP and POP for future mailboxes" -ForegroundColor DarkYellow
Get-CASMailboxPlan -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" } | set-CASMailboxPlan -ImapEnabled $false -PopEnabled $false
$confirmPlans = Get-CASMailboxPlan -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" }
if (!$confirmPlans) {
    Write-Host "IMAP and POP disabled for all future mailboxes" -ForegroundColor Green
}
else {
    Write-Host "IMAP and POP not disabled for all existing mailboxes" -ForegroundColor Red
}

Write-Host "Disabling IMAP and POP on all existing mailboxes" -ForegroundColor DarkYellow
Get-CASMailbox -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" } | Select-Object @{n = "Identity"; e = {$_.primarysmtpaddress}} | Set-CASMailbox -ImapEnabled $false -PopEnabled $false
$confirmMailboxes = Get-CASMailbox -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" }
if (!$confirmMailboxes) {
    Write-Host "IMAP and POP disabled on all existing mailboxes`n" -ForegroundColor Green
}
    
Remove-PSSession $Session

How to disable IMAP and POP in all customer Office 365 tenants via PowerShell

$credential = Get-Credential
Connect-MsolService -Credential $credential

$customers = Get-MsolPartnerContract -All

foreach ($customer in $customers) {

    Write-Host "Attempting IMAP and POP operations on $($customer.name)" -ForegroundColor Yellow
    $InitialDomain = Get-MsolDomain -TenantId $customer.TenantId | Where-Object {$_.IsInitial}
    $DelegatedOrgURL = "https://outlook.office365.com/powershell-liveid?DelegatedOrg=" + $InitialDomain.Name
    $EXODS = New-PSSession -ConnectionUri $DelegatedOrgURL -Credential $credential -Authentication Basic -ConfigurationName Microsoft.Exchange -AllowRedirection
    Import-PSSession $EXODS -CommandName Get-CASMailbox, Set-CasMailbox, Get-Casmailboxplan, set-casmailboxplan
    
    Write-Host "Disabling IMAP and POP for future mailboxes" -ForegroundColor DarkYellow
    Get-CASMailboxPlan -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" } | set-CASMailboxPlan -ImapEnabled $false -PopEnabled $false
    $confirmPlans = Get-CASMailboxPlan -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" }
    if (!$confirmPlans) {
        Write-Host "IMAP and POP disabled for all future mailboxes" -ForegroundColor Green
    }
    else {
        Write-Host "IMAP and POP not disabled for all existing mailboxes" -ForegroundColor Red
    }

    Write-Host "Disabling IMAP and POP on all existing mailboxes" -ForegroundColor DarkYellow
    Get-CASMailbox -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" } | Select-Object @{n = "Identity"; e = {$_.primarysmtpaddress}} | Set-CASMailbox -ImapEnabled $false -PopEnabled $false
    $confirmMailboxes = Get-CASMailbox -Filter {ImapEnabled -eq "true" -or PopEnabled -eq "true" }
    if (!$confirmMailboxes) {
        Write-Host "IMAP and POP disabled on all existing mailboxes`n" -ForegroundColor Green
    }
    
    Remove-PSSession $EXODS
}

Was this article helpful?

Related Articles