LiquidObject

PowerShell Scripting Signing »« Exchange Set-OutOfOffice

Removing old SMTP Addresses in Exchange

If you’ve ever changed domain names or changed e-mail addresses within an organization usually there are always some leftovers which no one wants to clean up because it too much of a headache. For example smithj@testdomain.local changed to smithj@mynewdomain.local along with thousands of other users. Now after removing the old relaying information and MX records the addresses are no longer valid. Wrong, they are still valid for are users within the same Exchange Organization. Now to clean the addresses out for good you can process them one at a time or run something similar to the below.

In this sample script I have it divided up only to run on certain mailbox databases at a time, this is handy if you are consolidating multiple exchange organizations into a single organization where some users already exist and do not need to be looked at.

 

$mydb = "Employees_5"
$mydb = "Employees_6"
$mydb = "Employees_7"
$mydb = "Employees_8"
$mydb = "Employees_12"
$mydb = "Employees_15"
$icount = 0


if((Get-PSSnapin Microsoft.Exchange.Management.PowerShell.*).count -lt 2) {Add-PSSnapin Microsoft.Exchange.Management.PowerShell.*}
sleep 1

$mailbox = get-mailbox -Database "$mydb" -resultsize unlimited
$mailbox | foreach {
for ($i=0;$i -lt $_.EmailAddresses.Count; $i++)
{
    $address = $_.EmailAddresses[$i]
    if($_.EmailAddresses.Count -gt 1)
    {
    Write-host $_.SamAccountName","$_.EmailAddresses","$_.EmailAddresses.Count
        $i++
    }
    
    if ($address.SmtpAddress -notlike "*@liquidobject.com" )
    {
        Write-host("Removed smtp address: " + $address.AddressString.ToString() )
        $icount++
        $_.EmailAddresses.RemoveAt($i)
    }
}
write-host "Total of: " $icount " users in DB: $mydb"
, ,
April 16, 2012 at 10:22 am
Leave a Reply or trackback