PowerShell: Remove Email Address with .local suffix

I am attempting to take a list of users via CSV and remove all email addresses with .local in it.
Via AD PowerShell, Exchange Management Shell or Quest Shell I would like to loop through a CSV to accomplish this
LVL 9
K BAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

David SankovskySenior SysAdminCommented:
Are we talking about an AD property or an actual Exchange email address domain? (Where the domain is *.local)

If it's the 2nd one, why would you need a CSV? you can simply spool through your entire list of users and use this PS script

foreach($i in Get-Mailbox -ResultSize Unlimited) {
  $i.EmailAddresses |
    ?{$_.AddressString -like '*@yourdomain.local'} | %{
      Set-Mailbox $i -EmailAddresses @{remove=$_}
    }
}

Open in new window


This will remove entries that has *@yourdomain.local from all mailboxes while leaving all other email addresses intact, perfect for situations where a certain OU had two domain suffixes and one was decommissioned.

Hope that helps.
K BAuthor Commented:
It's just a small amount of users I need to do this for. I need to leave multiple users untouched hence the CSV
David SankovskySenior SysAdminCommented:
I see.

In that case you might need to create a has table based on the initial data and loop through that.
However I'm afraid I'm in and out of meetings for the upcoming several hours. if no one will provide a solution till then I'll try to whip up a script for you. can you supply me with a dummy CSV for testing?
K BAuthor Commented:
Thank you David.  The CSV I speak of would be the user's samAccountName or UPN and that's it (I believe). Please tell me if this makes sense. I could put together a sample CSV but it would just be something like this:

John
Fred
Harry
Sam
Bryce


Or...

John@contoso.com
Fred@contoso.com
Harry@contoso.com
Sam@contoso.com
Bryce@contoso.com


----

Most of the users in the list will have a contoso.local in their proxyaddresses (AD) (or email addresses in Exchange (2010)). We also have Quest cmdlets at our disposal. AD, Exchange or Quest cmdlets would be great.

Thank you again!
K.B.
David SankovskySenior SysAdminCommented:
I'm afraid I must confess AD scripts are not my strongest suit. the Exchange Script should be easy enough to figure out though, Seeing as my original script will do the trick, we only have to change the loop through to loop through hash table.
So basically, we could in theory use this:
 
$users=Import-Csv $csvfile

foreach($user in $users){

$user.EmailAddresses | where{$_.AddressString -like '*@contoso.com'}| foreach{

Set-mailbox $user -EmailAddresses @{remove=$_}

   }

}

Open in new window

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
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
Powershell

From novice to tech pro — start learning today.