Powershell One-liner for finding all objcets that use a spesific domain name in Exchange Online

In some cases we need to find all objects that use a domain name in Exchange Online or Azure AD. For example when we need to remove a domain from a Office 365 tenant. This is not a easy task, unless we are able to script it.

The following three one-liners prints out all email addresses that is active on a object.

Get all users with @mydomain.com

Get-MsolUser -DomainName "mydomain.com"

Get all distribution groups with @mydomain.com

Get-DistributionGroup | foreach {$_.EmailAddresses | ? {$_ -like "*@mydomain.com"}}

Get all dynamic distribution groups with @mydomain.com

Get-DynamicDistributionGroup | foreach {$_.EmailAddresses | ? {$_ -like "*@mydomain.com"}}

Thereafter we can either go in and remove these manually, or we could write a new script that does it for us using the Set-MailUser, Set-Mailbox, Set-DistributionGroup or Set-DynamicDistributionGroup

Advertisements

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s