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’ }


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’ }


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!


The Energized Tech