Why not start with Get-Recipient and Where-Object (as above)? Pipe the results into a ForEach-Object where you can examine the properties of each object to determine its type (mailbox, mailuser, mailcontact, etc.) and then use the appropriate Set-* cmdlets (based on the type of object) to remove the addresses?
Powershell script to delete specific domain suffix from all mailbox smtp email addresses

I'm working on a PowerShell script to bulk remove any reference to a specific domain suffix in office 365 objects. The objects include office 365 groups, user email addresses, distribution lists, dynamic distribution lists, resources, shared mailboxes and user mailbox. The ultimate goal is to ensure that the domain does not have any reference in any of office 365 objects.
Have done dozen of research and i seem to find many script that only removes any email addresses associated to a specific domain pulled by this -EmailAddresses
parameter when using get-mailbox
and set-mailbox
cmdlet. This works fine, however it only targets user mailboxes email addresses but not the other objects mentioned above.
The script is this one
` $mailboxes=get-mailbox -resultsize unlimited
foreach($mailbox in $mailboxes)
{
for($i=($mailbox.EmailAddresses.count)-1; $i -ge 0; $i--)
{
$address=$mailbox.EmailAddresses[$i]
$addressString=$address.addressString
if($addressString -like "*$($_.samaccountname)@berniesoft.com*")
{
$mailbox.EmailAddresses.removeat($i)
}
}
$mailbox|set-mailbox -EmailAddresses $mailbox.EmailAddresses
}`
The requirement is to ensure that this script get-recipient | where {$_.EmailAddresses -match "berniesoft.com"} | fl Name, RecipientType, EmailAddresses
returns nothing for that domain.
If someone has a similar script implementation, you can share here. Thank you.
Exchange Online
Windows for business | Windows Server | User experience | PowerShell
9 answers
Sort by: Most helpful
-
-
Limitless Technology 39,921 Reputation points
2021-10-04T12:25:37.353+00:00 Hello,
maybe something like mentioned above to start with Get-Mailboxes:
First get all mailboxes
$mailboxes = get-mailbox;For each mailbox as mentioned
foreach ($mailbox in $mailboxes) { $emailaddresses = $mailbox.emailaddresses; Each SMTP address found on each mailbox for ($i=0; $i -lt $emailaddresses.count; $i++) { Just change the domain name below to what you want to remove if ($emailaddresses[$i].smtpaddress -like "*domain.name.com*") { And remove the unwanted email address $badaddress = $emailaddresses[$i]; $emailaddresses = $emailaddresses - $badaddress; $mailbox | set-mailbox -emailaddresses $emailaddresses; } } }
--If the reply is helpful, please Upvote and Accept as answer--
-
Rich Matheisen 47,901 Reputation points
2021-10-04T19:00:07.537+00:00 Sticking to just updating one mailbox, this might work:
$samaccountname = "JoeSchmoe" $mbx = Get-Mailbox <name> $update = $false [array]$NewAddr = @() $mbx.EmailAddresses | ForEach-Object { if ($_ -notlike "*$******@berniesoft.com*") { # be sure that the leading "Type:" is part of the AddressString!!!! $NewAddr += $_ # keep this address } else { $update = $true # and don't keep the $******@berniesoft.com one! } } # You should check to be sure that there's still a PRIMARY SMTP address # and if there isn't, provide for either inserting another address with a capitalized "SMTP:" or picking one address in $NewAddr and making IT the primary if ($update) { $mbx | Set-Mailbox -EmailAddresses $newAddr }
I don't have an Exchange organization to work with any more, so I can't verify the above code! But I was an Exchange MVP for about 16 years before retiring so the code is at least roughly correct (unless retirement has severely eroded my memory!).
-
Benard Mwanza 1,006 Reputation points
2021-10-06T13:25:57.35+00:00 Find attached the snipshot of errors that i'm getting on running the second script you provided.
-
Benard Mwanza 1,006 Reputation points
2021-10-06T13:33:43.43+00:00 This is the logic i need to implement.
CURRENT_VANITY.com is the undesired domain.
For each Object { If the primary address is <CURRENT_VANITY.COM> { If an <ON_MICROSOFT.COM> alias does not exist Add an alias with <ON_MICROSOFT.COM> Set <ON.MICROSOFT.COM> address as primary } Delete all secondary addresses with <CURRENT_VANITY.COM> and only those ones }