question

Sop-3080 avatar image
0 Votes"
Sop-3080 asked Samdi-4478 answered

Bulk Remove secondary SMTP address PowerShell script help.

Hello Guys,

Can someone please take a look at the script that i'm using to bulk remove secondary smtp address for all the mailboxes in a DistributionGroup.


Start-transcript -path C:\Users\User1\Desktop\Dry-Run\remove-smpt-address-5.log -append

Get all mailboxes


$Mailboxes = Get-DistributionGroupMember -Identity "Marketing" -ResultSize unlimited


Loop through each mailbox

foreach ($Mailbox.name in $Mailboxes) {


 # Add infoscites.com
 $Mailbox.EmailAddresses | Where-Object { $_.AddressString -like "*@xyz.com" } | ForEach-Object {
 
     # Remove the -WhatIf parameter after you tested and are sure to remove the secondary email addresses
     Set-Mailbox $Mailbox -EmailAddresses @{remove = $_ } -WhatIf
 
     # Write output
     Write-Host "Removing $_ from $Mailbox Mailbox" -ForegroundColor Green
 }

}

Stop-Transcript



The error that i'm getting is:

Cannot process argument transformation on parameter 'Identity'. Cannot convert the "domain.com/Users/Doe, Jame" value of type "Microsoft.Exchange.Data.Directory.Management.ReducedRecipient" to type
"Microsoft.Exchange.Configuration.Tasks.MailboxIdParameter".
+ CategoryInfo : InvalidData: (:) [Set-Mailbox], ParameterBindin...mationException
+ FullyQualifiedErrorId : ParameterArgumentTransformationError,Set-Mailbox
+ PSComputerName : exchangeserver.domain.com

windows-server-powershelloffice-exchange-server-administrationoffice-exchange-online-itprooffice-exchange-server-mailflow
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered IanXue-MSFT edited

Hi,

Please check to see if this works.

 foreach ($Mailbox in $Mailboxes) {
     $Mailbox.EmailAddresses | Where-Object { $_.AddressString -like "*@xyz.com" } | ForEach-Object {
          # Remove the -WhatIf parameter after you tested and are sure to remove the secondary email addresses
         Set-Mailbox $Mailbox.Name -EmailAddresses @{remove = $_ } -WhatIf
         # Write output
         Write-Host "Removing $_ from $Mailbox Mailbox" -ForegroundColor Green
     }
 }

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Samdi-4478 avatar image
0 Votes"
Samdi-4478 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.