Bulk Remove secondary SMTP address PowerShell script help.

Sophia 361 Reputation points
2021-05-11T05:47:33.96+00:00

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
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,521 questions
Exchange Server Management
Exchange Server Management
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Management: The act or process of organizing, handling, directing or controlling something.
7,628 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,521 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 36,751 Reputation points Microsoft Vendor
    2021-05-11T07:47:44.967+00:00

    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.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Samdi 41 Reputation points
    2021-08-04T06:48:20.217+00:00
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.