Remove SMTP Address from all Exchange 2010 User Mailboxes Using Powershell

I need a script that will remove a specific SMTP address from all of our users mailboxes.  The address is username@migration.company.com and was used during a coexistence/migration scenario between GroupWise and Exchange.  The username is specific to the person, so something like an if contain "migration.company.com" statement then delete SMTP address.  I'm an amateur to PowerShell so would need something cut and paste like.

Thanks in advance!
LVL 1
fireguy1125Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nick RhodeIT DirectorCommented:
Was there a policy created in exchange to populate these addresses?

Could just use ADModify:  http://buenoflex.com/archives/242
0
David CarrCommented:
You can try the following. Copy and paste into Notepad and save as SMTPRemove.ps1.
Go to Exchange Managment Shell and change to the location where you saved the script. type .\SMTPRemove.ps1 and press Enter

#########################################################
# This Script remove post-migration SMTP Addresses
cls
$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 "*migration.company.com")
         {
        $mailbox.EmailAddresses.removeat($i)
          }
    }
 
$mailbox|set-mailbox -EmailAddresses $mailbox.EmailAddresses
}

Open in new window

0
fireguy1125Author Commented:
NRhode -thanks so much, however unfortunately I'm unable to use any 3rd party utilities

gkrew - i've tried your script in our lab and seems to be exactly what we need. - Is there any way we can have the results of which mailboxes had the address removed from into a csv/txt file?
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Nick RhodeIT DirectorCommented:
Just in case for after you run the script if those emails were created by a policy you would have to remove/disable that policy otherwise it might update and put the addresses back onto the mailboxes.
0
David CarrCommented:
Here is the udpated script

###############################################################
#This Script will remove post-migration SMTP Addresses and creates a time and 
# date stamped text file when it runs.

cls
###############################################################
$startTime = $time
# date and time and logging
$dateTime = Get-Date
$hour = $dateTime.Hour
$hourIndex = $hour.ToString().Length
if ($hourIndex -eq 1)
{
                $hour = "0" + $hour.ToString()
}

$minute = $dateTime.Minute
$minuteIndex = $minute.ToString().Length
if ($minuteIndex -eq 1)
{
                $minute = "0" + $minute.ToString()
}
                
$second = $dateTime.Second
$secondIndex = $second.ToString().Length
if ($secondIndex -eq 1)
{
                $second = "0" + $second.ToString()
}

$date = $dateTime.ToShortDateString()
$date = $date.Replace("/", ".")
$time = $hour.ToString() + "." + $minute.ToString() + "." + $second.ToString()

$stopTime = $time

$logDateTime = $date + "_" + $time
#$logFile = "c:\Temp\SMTPRemoval_" + $logDateTime + ".txt"

$testPath = Test-Path "c:\Temp\SMTPRemoval"
if(!($testPath))
{
                new-item "c:\temp\SMTPRemoval" -itemtype directory
}

                $date = Get-Date
                $hour = $date.Hour
                $delete24Hours = $date.AddHours(-24)
                $delete12Hours = $date.AddHours(-12)

# /date and time and logging
##############################################################

cls
$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 "*migration.company.com")
         {
        $mailbox.EmailAddresses.removeat($i)
$info >> $logFile   
       }
    }
$mailbox|set-mailbox -EmailAddresses $mailbox.EmailAddresses
}
##########################################################

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexandre SantosCommented:
Hi,

The script was apply with success.

How to execute in all items do exchange 2010 SP3?  

tks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Exchange

From novice to tech pro — start learning today.