If a user with Full Access mailbox permission has left the organization, you need to…
How to Remove email addresses with PowerShell
To delete single or multiple email addresses from a specific mailbox in Microsoft 365, you can use the Exchange admin center or Microsoft 365 admin center. However, a PowerShell script can remove multiple email addresses from all mailboxes, which saves a lot of time. In this article, you will learn how to remove email addresses with PowerShell.
Table of contents
- Manage email addresses in Exchange Online
- Remove email addresses
- Remove email addresses in Exchange admin center
- Connect to Exchange Online PowerShell
- Remove specific email address from single mailbox with PowerShell
- Remove Primary SMTP email address from single mailbox
- Bulk remove all secondary smtp email addresses from single mailbox
- Bulk remove all secondary smtp email addresses from all mailboxes
- Bulk remove Primary SMTP address from all mailboxes
- Conclusion
Manage email addresses in Exchange Online
To manage email addresses in your Microsoft 365 organization, use the following articles:
- Add email addresses with PowerShell
- Remove email addresses with PowerShell (this article)
- Export email addresses with PowerShell
Remove email addresses
There are three methods to remove email addresses from a mailbox:
- Microsoft 365 admin center
- Exchange admin center
- Exchange Online PowerShell
The Microsoft 365 admin center only shows the primary SMTP address and the aliases, which are all the secondary and additional email addresses. In the Exchange admin center, there is detailed information about each email address, such as SMTP, smtp SIP, Proxy, or x500.
We will use the Set-Mailbox cmdlet to remove an email address in PowerShell.
Remove email addresses in Exchange admin center
- Sign in to Exchange admin center
- Click Recipients>Mailboxes
- Click on a mailbox
- Click Manage email address types
- Click on the recycle bin icon to remove the email addresses
- Click Save
In the next step, we will show how to remove an email address in PowerShell for a single mailbox and all mailboxes.
Connect to Exchange Online PowerShell
To run the PowerShell commands specified in the current article, you must Connect to Exchange Online PowerShell.
Connect-ExchangeOnline
Remove specific email address from single mailbox with PowerShell
To remove an email address, use the Set-Mailbox cmdlet with the @{remove parameter.
In our example, we want to remove an alias address of a specific user (Brenda.Smith@m365info.com).
Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses @{remove = "brenda.alias2@m365info.com" }
It will only remove this specific email address, and all the other email addresses remain.
You can also use the minus (–) symbol instead of remove but enclose the minus symbol in quotes.
Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses @{"-" = "brenda.alias2@m365info.com" }
Remove Primary SMTP email address from single mailbox
There is no option to delete the primary SMTP address because each mailbox needs to have one primary SMTP address.
The only way is to add a new primary SMTP address, which will save the old primary SMTP address as a secondary smtp address. Then, you can always remove the old smtp addresses from the mailbox.
You can also Change Microsoft 365 primary SMTP address in Microsoft 365 admin center and PowerShell.
Method 1. Remove Primary SMTP address and all other email addresses
Use the -EmailAddresses parameter in the PowerShell command to add the new primary SMTP email address and remove the other existing email addresses.
Set-Mailbox "Brenda.Smith@m365info.com" -EmailAddresses SMTP:BrendaNewEmail@m365info.com
The PowerShell command results:
- The new SMTP email address (BrendaNewEmail@m365info.com) replaces the current primary email address.
- The old SMTP primary email address is saved as a secondary smtp email address.
- All the other additional email addresses, such as aliases, proxy, .onmicrosoft.com, and SIP addresses, are removed (deleted).
After you run the PowerShell command, you will get the below warning.
WARNING: Proxy address “Brenda.Smith@m365info.com” is used as WindowsLiveId. So it can’t be removed from list of email addresses. To remove it, first change the WindowsLiveId.
Method 2. Remove Primary SMTP address and keep all existing email addresses
Use the -WindowsEmailAddress parameter in the PowerShell command to add the new primary SMTP email address without removing all the other email addresses.
Set-Mailbox "Brenda.Smith@m365info.com" -WindowsEmailAddress BrendaNewEmail2@m365info.com
The PowerShell command results:
- The new email address (BrendaNewEmail2@m365info.com) replaces the current primary email address.
- The old SMTP primary email address is saved as a secondary smtp email address.
- All the other additional email addresses, such as aliases, proxy, .onmicrosoft.com, and SIP addresses, remain intact.
Bulk remove all secondary smtp email addresses from single mailbox
You can remove all the smtp email addresses from a single user with a specific domain name.
In our example, we want to remove all the smtp email addresses with the domain m365info.com from the user (Brenda.Smith@m365info.com). All the other additional email addresses, such as proxy, .onmicrosoft.com, and SIP addresses, will remain intact.
Remove all the smtp email addresses of a specific user by following these steps:
- Specify the domain in line 2
- Specify the user in line 5
- Run the below PowerShell script
# Define the domain you want to remove
$DomainToRemove = "m365info.com"
# Get the mailbox
$Mailbox = Get-Mailbox -Identity "Brenda.Smith@m365info.com"
# Iterate through each email address associated with the mailbox
foreach ($EmailAddress in $Mailbox.EmailAddresses) {
# Check if the email address matches the domain you want to remove
if ($EmailAddress -clike "smtp:*@$DomainToRemove") {
# Remove the secondary smtp email addresses
$MailboxID = $Mailbox.PrimarySmtpAddress
Set-Mailbox "$MailboxID" -EmailAddresses @{remove = $EmailAddress }
Write-Host "The following email address $EmailAddress was removed from $MailboxID mailbox" -ForegroundColor Green
}
}
The PowerShell output shows the below result.
The following email address smtp:brenda.s@m365info.com was removed from Brenda.Smith@m365info.com mailbox
The following email address smtp:brenda2@m365info.com was removed from Brenda.Smith@m365info.com mailbox
Bulk remove all secondary smtp email addresses from all mailboxes
Let’s say your organization has different domain names, and you want to remove all the smtp email addresses with a specific domain name suffix.
In our example, we want to remove all the email addresses with the domain name m365info.com. The PowerShell script will only remove the smtp email addresses with the specific domain name (m365info.com). All the other additional email addresses, such as proxy, .onmicrosoft.com, and SIP addresses, will remain.
To remove all the smtp email addresses from all the mailboxes in an organization, you need to follow these steps:
- Specify the domain in line 2
- Run the below PowerShell script
# Define the domain you want to remove
$DomainToRemove = "m365info.com"
# Get the mailbox
$Mailboxes = Get-Mailbox -ResultSize Unlimited
# Iterate through each email address associated with the mailbox
foreach ($Mailbox in $Mailboxes) {
# Iterate through each email address associated with the mailbox
foreach ($EmailAddress in $Mailbox.EmailAddresses) {
# Check if the email address matches the domain you want to remove
if ($EmailAddress -clike "smtp:*@$DomainToRemove") {
# Remove the secondary smtp email addresses
$MailboxID = $Mailbox.PrimarySmtpAddress
Set-Mailbox "$MailboxID" -EmailAddresses @{remove = $EmailAddress }
Write-Host "The following email address $EmailAddress was removed from $MailboxID mailbox" -ForegroundColor Green
}
}
}
The PowerShell output shows the below results.
The following email address smtp:adam.m@m365info.com was removed from Adam.Mackay@m365info.com mailbox
The following email address smtp:andrea2@m365info.com was removed from Andrea.Baker@m365info.com mailbox
The following email address smtp:blake2@m365info.com was removed from Blake.Martin@m365info.com mailbox
The following email address smtp:blake@m365info.com was removed from Blake.Martin@m365info.com mailbox
The following email address smtp:carol@m365info.com was removed from Carol.Piper@m365info.com mailbox
Bulk remove Primary SMTP address from all mailboxes
You can’t remove a primary SMTP address without adding a new primary email address. Use the PowerShell script to remove all primary SMTP addresses with a specific domain name suffix from all mailboxes without deleting other email addresses.
In this example, it will remove all the primary SMTP email addresses with the domain (m365info.com). Then, it will add a new primary email address with another new domain (m365pilot.com) for all the mailboxes.
- Type the new domain name in line 3
- Run the below PowerShell script
$Mailboxes = Get-Mailbox -Filter { IsDirSynced -eq $false } -ResultSize Unlimited
foreach ($Mailbox in $Mailboxes) {
$NewAddress = $Mailbox.Alias + "@m365pilot.com"
if ($Mailbox.PrimarySmtpAddress -notlike "$NewAddress") {
Set-Mailbox -Identity $Mailbox.Alias -WindowsEmailAddress $NewAddress
Write-Host "Changed primary SMTP address $($Mailbox.PrimarySmtpAddress) to $NewAddress" -ForegroundColor Green
}
else {
Write-Host "Primary SMTP address $NewAddress is already set for $($Mailbox.PrimarySmtpAddress)" -ForegroundColor Yellow
}
}
The PowerShell script results:
- Replace the current primary email address with the domain name suffix m365pilot.com.
- It will not delete the previous primary SMTP address but change it to an alias (smtp) email address.
- All the other existing email addresses, such as Proxy, SIP, or x500 email addresses of the mailboxes, will remain.
That’s it!
Read more: How to remove Full Access mailbox permission »
Conclusion
You learned how to remove email addresses with PowerShell in Microsoft 365. Use the Exchange admin center or PowerShell to remove multiple email addresses from a single mailbox. But if you have to go through multiple mailboxes, it’s much faster with PowerShell.
Did you enjoy this article? You may also like How to Delete mail items from Microsoft 365 mailbox. Don’t forget to follow us and share this article.
This Post Has 0 Comments