Showing posts with label proxyaddress. Show all posts
Showing posts with label proxyaddress. Show all posts

Wednesday, August 3, 2011

More proxyaddresses scripts

Remember the post about Remove proxy addresses Script? It was developed for Exchange 2007 and did not work for Exchange 2010.
People wrote comments with suggestions (thank you all for that) and I thought I published an updated version that work with Exchange 2010 together with another script that add SMTP addresses.
# Remove proxy addresses
# change the Get-Mailbox statement in line 7 to select only a subset of mailboxes

$DomainToRemove = "*@olddomain.com"

#get mailboxes and iterate through
Get-Mailbox -ResultSize Unlimited | foreach {
$_.Alias
# .emailaddresses returns array
# loop each email address
for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
{
$address = $_.EmailAddresses[$i]
#Write-Host $address
# removes all addresses with $DomainToRemove
if ($address.SmtpAddress -like $DomainToRemove )
{
Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
# remove address in the array
$_.EmailAddresses.RemoveAt($i)
}
}
# save changes
$_ | Set-Mailbox -EmailAddresses $_.EmailAddresses
Write-Host
}






And the Add proxy addresses script.



# Add proxy addresses
# change the Get-Mailbox statement in line 8 to select only a subset of mailboxes

$AddressSearchedFor = "*@domaintocopy.com"
$DomainToAdd = "@newdomain.com"

#get mailboxes and iterate through
Get-Mailbox -ResultSize Unlimited | foreach{
$_.Alias
# .emailaddresses returns array
# loop each email address
for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
{
$address = $_.EmailAddresses[$i]
#Write-Host $address

# look for SMTP addresses in source
if ($address.SmtpAddress -like $AddressSearchedFor )
{
# get the left part of address
$a = [string] $address
$b = $a.indexof("@")
$a = $a.substring(5, $b-5 )
#Write-Host $

# Add SMTP address
Write-host("Adding smtp adress: " + $a + $DomainToAdd )
# add address in the array
$_.EmailAddresses.add("smtp:" + $a + $DomainToAdd)
}
# save changes
$_ | Set-Mailbox -EmailAddresses $_.EmailAddresses
}
Write-Host
}



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.