Powershell – Exchange 2007 – List emails matching wildcard for Primary SMTP

| | Comments (2)

Powershell

Dropping or adding a domain normally can be a headache.  Especially in the Enterprise where who KNOWS what the addresses were attached.  One of nastiest headaches in the past was trying to find out the default email address for a User.  in Exchange 5.5?  BHA!  Look at the objects one at a time, or get really good with vbScript and a bottle of Vodka!

But now in Powershell, life has changed for the better.

With ONE line!  ONE Line!  I can find all the users in a particular domain for the default email.  Distribution Groups *OR* Email addresses.  It doesn't matter.

One line.

All you have to use is the GET-MAILBOX or GET-DISTRIBUTIONLIST Cmdlets.  Each one of those holds the field "PrimarySMTPAddress".  But the nice part is that field is already a set of data.   So if you're looking for a user who's email end's in a particular domain (say ABC.COM) just run a quick

 

GET-MAILBOX * | where { $_.PrimarySMTPAddress.Domain -eq 'abc.com' }

or

GET-DISTRIBUTIONLIST * | where { $_.PrimarySMTPAddress.Domain -eq 'abc.com' }

But of course if you're dealing with over 10,000 plus users you might want to limit it to the odd OU.  That's quite easy.  Just add in the -ORGANIZATIONALUNIT parameter to the Cmdlet like so.  If we're in the fictional domain of "FABRIKAM.COM" trying to find our Distribution List from a particular OU.

 

GET-MAILBOX * -Organizationalunit 'Fabrikam.com/Office/Redmond/Mailgroups' | where { $_.PrimarySMTPAddress.Domain -eq 'abc.com' }

or

GET-DISTRIBUTIONLIST *  -Organizationalunit 'Fabrikam.com/Office/Redmond/Mailgroups' | where { $_.PrimarySMTPAddress.Domain -eq 'abc.com' }

 

And of course you can get really fancy too once you have this information.   You could just as EASILY modify that same Cmdlet and just SWITCH those users to the new email domain.  No muss.  No fuss!

 

Powershell, It really is so Easy to use!

 

Sean
The Energized Tech

2 Comments

In the text, it is mentioned "Just add in the –ORGANIZATIONUNIT parameter". There's a typo. It's OrganizationalUnit (missing the "al"). And that can actually be shorted to -org. Such as

GET-MAILBOX –Org "Fabrikam.com/Office/Redmond/Mailgroups" | where { $_.PrimarySMTPAddress.Domain –eq "abc.com" }

And, if "Mailgroups" is the only OU with that name, we could actually shorten the line further:

GET-MAILBOX –Org "Mailgroups" | where { $_.PrimarySMTPAddress.Domain –eq "abc.com" }

Hi Sean,

I would like to ask a question.
Instead of looking for primary smtp addresses
I want to look for all email addressess for a specific domain and remove them.

Due to a mail migration many old email addresses are migrated ans i want to remove them.

I tried to change the script

$_.PrimarySMTPAddress.Domain => $_.EmailAddresses

But this doesn't work, i'm quite new with scipting and i hope you could help me ith this.

Thanks in advance!

Regards,

Loki1974