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.

16 comments:

  1. Hi,

    just for info. Tried to get this script working, but there are a couple of problems with it.
    It seems, that a "PIPE" character is missing from lines 6 and 21 (guess it's because HTML restrictions on the site - at least with IE7?). Also the FOR cycle has a problem and misbehaves at certain situations.
    It should be:
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)

    But thanks for the script! Saved me some manual labor ;)

    ReplyDelete
  2. Good info, got the same information from other source.
    seems the website dont like "PIPE" character

    ReplyDelete
  3. This is the correct Script:

    Get-Mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    if ($address.SmtpAddress -like "*@target.local" )
    {
    Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    $_|set-mailbox
    }

    ReplyDelete
  4. Get-Mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    if ($address.SmtpAddress -like "*@target.local" )
    {
    Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    $_|set-mailbox
    }

    ReplyDelete
  5. Thank you, saved me a bunch of manual work

    ReplyDelete
  6. Thanks for sharing! :)

    ReplyDelete
  7. Nice script!

    I am trying to use in Exchange 2010 and keep recieving an error "Pipeline not executed because a pipeline is already executing." Microsofo notes thisis an issue in EX2010 and recommends using a variable in the Get-Mailbox statements as displayed below....

    Get-Mailbox | ForEach { Set-Mailbox -ProhibitSendReceiveQuota 3GB }

    To

    $Mailboxes = Get-Mailbox
    $Mailboxes | ForEach { Set-Mailbox -ProhibitSendReceiveQuota 3GB }

    I have tried with your script but modifying it to....

    $mailbox = Get-Mailbox
    $mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    if ($address.SmtpAddress -like "*@target.local" )
    {
    Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    $_|set-mailbox
    }

    and this resolves the pipe error and the script runs however the email addresses are still present on exit and it seems like the $mailbox = Get-Mailbox and the $mailbox |foreach sections are not working together.

    to clarify teh $mailbox = get-mailbox completes and presents the rest of the script and after pressing enter 2 time the rest of the script runs...

    I have 250+ users that have garbage entries in from a long ago dead and gone reciepient and need to get these address removed.

    any insight is welcome.

    regards,

    Sean

    ReplyDelete
  8. I got the same problem with this script in Exchange 2010and didnt have time to convert it to work with remote powershell.
    You can start a local powershell session and then run the script.
    Find the "Windows Powershell Modules"in start menu and click it. After it starts a local powershell session should have been started with Excange 2010 snapin loaded.
    And now you should be able to run scripts.
    This is not exactly supported but it works

    ReplyDelete
  9. Lasse,

    thanks for the prompt reply. the originail script runs through ok however when i review the email adresses in the Exchange Management Console teh addresses that should have been removed are still present.

    Any thoughts?

    Regards,

    Sean

    ReplyDelete
  10. Lasse,

    I have found a different tool to accomplish the task at hand in Exchange 2010. it is ADmodify.net from the codeplex.com site. (http://admodify.codeplex.com/)

    I have used this in the past to update the LegacyDN attribute when performing exchange migrations but it had slipped my mind until now.

    hope all you readers benefit from this tool.

    Regards,

    Sean

    ReplyDelete
  11. X400 Adress Removal

    Thanks for the script

    At Exchange 2007 x400-E-Mail Adresses may be no longer needed. So i used the script from this thread to remove them all, doing the following:


    Get-Mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    # removes all addresses Type x400
    if ($address.PrefixString -eq "x400" )
    {
    Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    $_ | set-mailbox
    }

    ReplyDelete
  12. Thanks guys - I got this script to work correctly in exchange 2010 by modifying the set-mailbox parameters.

    $mailbox = Get-Mailbox
    $mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    if ($address.SmtpAddress -like "*@test.domain" )
    {
    Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    $_|set-mailbox -EmailAddresses $_.EmailAddresses
    }

    ReplyDelete
  13. Remove X400 for Exchange 2010:

    $mailbox = Get-Mailbox -resultsize unlimited
    $mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    # removes all addresses Type x400
    if ($address.PrefixString -eq "x400" )
    {
    Write-host("Remove x400 address: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    $_ | set-mailbox -EmailAddresses $_.EmailAddresses
    }


    Remove legacy exchange 5.5 MDX for exchange 2010:
    $mailbox = Get-Mailbox -resultsize unlimited
    $mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    # removes all addresses Type x400
    if ($address.PrefixString -eq "x400" )
    {
    Write-host("Remove x400 address: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    $_ | set-mailbox -EmailAddresses $_.EmailAddresses
    }

    ReplyDelete
  14. Thanks for this script. Worked like a charm, saved me a lot of work

    ReplyDelete
  15. Wе're a group of volunteers and starting a new scheme in our community. Your website provided us with valuable information to work on. You have done an impressive job and our entire community will be thankful to you.

    Also visit my weblog :: how is technology more productive i ()

    ReplyDelete
  16. If you are getting the pipeline error, force a cooldown period for you script:

    get-content c:\users.txt | Get-Mailbox | foreach {
    for ($i=$_.EmailAddresses.Count;$i -ge 0; $i--)
    {
    $address = $_.EmailAddresses[$i]
    if ($address.SmtpAddress -like "*@plato.com" )
    {
    Write-host("Remove smtp adress: " + $address.AddressString.ToString() )
    $_.EmailAddresses.RemoveAt($i)
    }
    }
    start-sleep 1
    $_|set-mailbox
    start-sleep 5
    }

    ReplyDelete