Saturday, June 7, 2008

Remove proxyaddresses powershell script

A friend of mine approached me with a question about email addresses on his Exchange servers the other day. He wanted to remove some old SMTP addresses not used anymore and of course not doing this the manual way by clicking each recipient and remove the address, some sort of script was needed.
He is using Exchange 2007 so I figured a PowerShell script would do it. This is the PowerShell script I created. It can certainly be made more flexible and efficient, but this one still functions well.create a file “remove_proxyaddess.ps1” and edit line 6 and 13 to suit you environment

# Remove proxy addresses
# change the Get-Mailbox statement in line 6 to select only a subset of mailboxes
# change -like paratameter in line 13 to the domain you want to remove

#get mailboxes and iterate through
Get-Mailbox foreach {
# .emailaddresses returns array
# loop each email address
for ($i=0;$i -lt $_.EmailAddresses.Count; $i++)
{
$address = $_.EmailAddresses[$i]
# removes all addresses with test.com domain
if ($address.SmtpAddress -like "*@test.domain" )
{
Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
# remove address in the array
$_.EmailAddresses.RemoveAt($i)
}
}
# save changes
$_ set-mailbox
}

Run script from Exchange Management console. Script will output info to the console so adjusting screen buffer size to a large value is a good thing so you can scroll through output and see what happened when running script.