locked
delete secondary SMTP address input from a text file using powershell RRS feed

  • Question

  • Environment:

    Exchange 2007 SP3

    Users from various OU got @abc.com (primary) and @xyz.com (secondary), as they are not in one OU, cant use ADModify tool to remove the secondary SMTP address in one shot or i have to choose these users one by one...

    I got a list (CSV) with username and their email address for which removal for secondary SMTP address need to take place, i am looking for a script which can take input either from a CSV or from a text file, remove the SMTP address and give confirmation for all those whose removal is done sucessfully.

    Thanks


    Inderjit

    Thursday, March 22, 2012 2:34 PM

Answers

  • Hello Inderjit,

    Maybe you can follow these commands to have a try (no need to create the csv file):

    $users=get-mailbox -resultsize unlimited

    foreach($user in $users){

    $user.EmailAddresses | where{$_.AddressString -like '*@xyz.com'}| foreach{

    Set-mailbox $user -EmailAddresses @{remove=$_}

       }

    }

    If you need use csv file, you can go with this:

    $users=import-csv -path "csv file path"

    foreach($user in $users){

    $mailbox=get-mailbox -identity $user

    $mailbox.EmailAddresses | where{$_.AddressString -like '*@xyz.com'}| foreach{

    Set-mailbox $mailbox -EmailAddresses @{remove=$_}

       }

    }

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support


    Monday, March 26, 2012 8:57 AM
    Moderator

All replies

  • Environment:

    Exchange 2007 SP3

    Users from various OU got @abc.com (primary) and @xyz.com (secondary), as they are not in one OU, cant use ADModify tool to remove the secondary SMTP address in one shot or i have to choose these users one by one...

    I got a list (CSV) with username and their email address for which removal for secondary SMTP address need to take place, i am looking for a script which can take input either from a CSV or from a text file, remove the SMTP address and give confirmation for all those whose removal is done sucessfully.

    Thanks


    Inderjit

    • Merged by Gavin-Zhang Monday, March 26, 2012 8:19 AM duplicate
    Thursday, March 22, 2012 2:26 PM
  • Hello,

    You can try this one: http://community.spiceworks.com/scripts/show/741-remove-secondary-smtp-and-secondary-proxyaddress-mailboxes-version

    To read CSV file content using Powershell, use Import-Csv cmdlet. More here: http://technet.microsoft.com/en-us/library/ee176874.aspx


    This posting is provided "AS IS" with no warranties or guarantees , and confers no rights.   

    Microsoft Student Partner 2010 / 2011
    Microsoft Certified Professional
    Microsoft Certified Systems Administrator: Security
    Microsoft Certified Systems Engineer: Security
    Microsoft Certified Technology Specialist: Windows Server 2008 Active Directory, Configuration
    Microsoft Certified Technology Specialist: Windows Server 2008 Network Infrastructure, Configuration
    Microsoft Certified Technology Specialist: Windows Server 2008 Applications Infrastructure, Configuration
    Microsoft Certified Technology Specialist: Windows 7, Configuring
    Microsoft Certified Technology Specialist: Designing and Providing Volume Licensing Solutions to Large Organizations
    Microsoft Certified IT Professional: Enterprise Administrator
    Microsoft Certified IT Professional: Server Administrator
    Microsoft Certified Trainer

    Thursday, March 22, 2012 2:59 PM
  • Thanks X

    I already tried the one above, it gave me a warning when i replace"*SecondarySMTP*" with either xyz.com or xyz, that "The command completed successfully but no settings of 'domain/OU/user alias' have been modified.

    Nothing changed at Exchange :(

    Thanks


    Inderjit

    Thursday, March 22, 2012 3:11 PM
  • I have rights...using domain admin account.

    Inderjit

    Thursday, March 22, 2012 3:12 PM
  • In your CSV, all you will need is a header of 'username' and a list of the users SamAccountNames for my script to work.  I tried to make it self explanatory, but if you have questions, let me know.  Just copy the script below, paste it into a notepad, and save it as a .ps1, modify the fields for domain to remove and path, and you are good.  I would reccomend creating a test CSV with one or two users first and do a dry run, or you can uncomment line 52 and comment out line 54 if you want to see the output without making any changes, or just add -WhatIf to line 54.

    #Begin Cut Below

    ####################################################
    #
    # This script takes input from a CSV with a single
    # column that has a header of 'username' and parses
    #   all email addresses for that user and removes the
    #   email addresses that match the $RemDomain.
    #
    #   Author: Russ Burden, Jr.
    #   Date: 03/22/2012
    #
    #   AS USUAL, THE USE OF THIS SCRIPT IS AT YOUR OWN
    # RISK, I PROVIDE NO GUARANTEE OR WARRANTY FOR IT.
    # IT IS RECCOMMENDED THAT YOU DO TESTING BEFORE
    # USING THIS IN A PRODUCTION ENVIRONMENT.
    #
    ####################################################

    #If an EmailAddressPolicy exists for the domain you wish to remove email addresses for, the system will just
    #put them back after this script runs

    Write-Host "Ensure there is not a Email Address Policy in place for the domain you wish to remove"

    #Define the domain you want to remove below
    $RemDomain = "@domaintoremove.com"

    #Insert the path and filename to your CSV below
    $InputCSV = Import-CSV -Path "PathToFile.CSV"

    #Step through all users imported from the CSV
    foreach ($line in $InputCSV) {

     $UserID = $line.username

     Try {
      #Get the users mailbox
      $UserMailbox = Get-Mailbox $UserID -ErrorAction Stop
      
      #Create a fresh ArrayList for each user
      
      $EmailList = New-Object System.Collections.ArrayList
      
       #Step through each email address
       foreach ($Address in $UserMailbox.EmailAddresses) {
        $EmailAddress = $Address.ProxyAddressString
        
        #If the email address does not match the domain to remove, add it to the ArrayList
        If ($EmailAddress -notlike "*"+$RemDomain) {
         $EmailList.Add($EmailAddress) | out-null
        }
       }
      #Test Output, Uncomment line below to output email address to the console
      #$EmailList | FL
      #Write the ArrayList (email address) to the users mailbox
      Set-Mailbox $UserID -EmailAddresses $EmailList
     }
     Catch {
      #Build error message and write it out if there was an issue retrieving the users mailbox
      $ErrMessage = "Please verify mailbox for " + $UserID
      Write-Host $ErrMessage
     }
    }

    #End Cut

    Thursday, March 22, 2012 4:57 PM
  • On Thu, 22 Mar 2012 14:34:39 +0000, IJSingh wrote:
     
    >
    >
    >Environment:
    >
    >Exchange 2007 SP3
    >
    >Users from various OU got @abc.com (primary) and @xyz.com (secondary), as they are not in one OU, cant use ADModify tool to remove the secondary SMTP address in one shot or i have to choose these users one by one...
     
    "dc=domain,dc=tls" is a valid root to used in admodify. A subtree
    search from there will give you the entire forest if you use the
    forest root domain.
     
    >I got a list (CSV) with username and their email address for which removal for secondary SMTP address need to take place, i am looking for a script which can take input either from a CSV or from a text file, remove the SMTP address and give confirmation for all those whose removal is done sucessfully.
     
    You've removed the secondary domain from all youe email address
    policies, right?
     
    ---
    Rich Matheisen
    MCSE+I, Exchange MVP
     

    --- Rich Matheisen MCSE+I, Exchange MVP
    Thursday, March 22, 2012 9:19 PM
  • Thanks for the reply.

    yes, the policies has already been removed for xyz.com.

    I will try the script posted above will let you know the results in a day or two.

    Thanks


    Inderjit

    Monday, March 26, 2012 7:58 AM
  • Hello Inderjit,

    Maybe you can follow these commands to have a try (no need to create the csv file):

    $users=get-mailbox -resultsize unlimited

    foreach($user in $users){

    $user.EmailAddresses | where{$_.AddressString -like '*@xyz.com'}| foreach{

    Set-mailbox $user -EmailAddresses @{remove=$_}

       }

    }

    If you need use csv file, you can go with this:

    $users=import-csv -path "csv file path"

    foreach($user in $users){

    $mailbox=get-mailbox -identity $user

    $mailbox.EmailAddresses | where{$_.AddressString -like '*@xyz.com'}| foreach{

    Set-mailbox $mailbox -EmailAddresses @{remove=$_}

       }

    }

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support


    Monday, March 26, 2012 8:57 AM
    Moderator
  • Hello Inderjit,

    Maybe you can follow these commands to have a try (no need to create the csv file):

    $users=get-mailbox -resultsize unlimited

    foreach($user in $users){

    $user.EmailAddresses | where{$_.AddressString -like "*@xyz.com'}| foreach{

    Set-mailbox $user -EmailAddresses @{remove=$_}

       }

    }

    If you need use csv file, you can go with this:

    $users=import-csv -path "csv file path"

    foreach($user in $users){

    $mailbox=get-mailbox -identity $user

    $mailbox.EmailAddresses | where{$_.AddressString -like "*@xyz.com'}| foreach{

    Set-mailbox $mailbox -EmailAddresses @{remove=$_}

       }

    }

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support


    Nice Evan, I'm always looking for ways to streamline PS code and didn't realize I could use the remove function, I believe I can incorporate that into a receive connector script I have been working on. The only reason I proceeded with the CSV the way I did is because I have no idea of how many users his system has on it. Once you get over a few thousand, getting all mailboxes becomes slow.
    Monday, March 26, 2012 1:03 PM
  • Awesome, thanks for your help, will try and let you know.

    I got approx 800+ users with secondary SMTP address configured, the reason why we need to have a CSV as an input because we would like to run the removal in phases, first all the users, second with DL third goes the special accounts

    The CSV will have users in it on which script will run to clear the secondary objects, will see if we can use the same for DL and Special Mailbox.

    Thanks

    -Inderjit


    Inderjit

    Wednesday, March 28, 2012 8:47 AM
  • Hi,

    For the DL, you can change commands "Get-mailbox" and "Set-mailbox" to "Get-distributiongroup" and "Set-distributiongroup".

    Get-mailbox also will return the special mailbox.

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support

    Thursday, March 29, 2012 3:47 AM
    Moderator
  • Any updates?

    Thanks,

    Evan


    Evan Liu

    TechNet Community Support

    Sunday, April 1, 2012 3:38 AM
    Moderator