Most of the customers I work with have just a small number of domain names used for email in their organization. But every now and then I do some work with a customer who has a very large number of email domains.

Often I’m interested in checking the MX records configured for all of those domain names. One example is during an Office 365 migration, when all of the MX records are planned to be moved from their on-premises infrastructure to Exchange Online Protection.

Querying all of the MX records manually would be a long and tedious task. And it would be very boring to have to manually repeat the task at different stages of the migration project.

Fortunately we can query MX records using the Resolve-DnsName cmdlet in PowerShell. Here’s an example:

[PS] C:\>Resolve-DnsName -Name -Type MX | ft -auto

Name          Type TTL Section NameExchange                              Preference
----          ---- --- ------- ------------                              ---------- MX   10  Answer 10

Here’s an example of a domain name with multiple MX records:

[PS] C:\>Resolve-DnsName -Name -Type MX

Name          Type TTL Section NameExchange                   Preference
----          ---- --- ------- ------------                   ---------- MX   598 Answer      10 MX   598 Answer      10 MX   598 Answer 20 MX   598 Answer      10 MX   598 Answer 20 MX   598 Answer 20

As you can see some domain names have one MX record, while others have several. So any script needs to handle multiple domain names *and* multiple MX records.

There’s a few ways we can handle querying multiple domain names. Basically we first need to capture the list of domain names into a variable. If the list of domain names was in a text file we could do this with Get-Content:

[PS] C:Scripts>$domains = @(Get-Content .domains.txt)

If we want to get the list of domain names from the Exchange organization itself we can use Get-AcceptedDomain:

[PS] C:Scripts>$domains = @((Get-AcceptedDomain).DomainName)

Now we simply pipe $domains into Resolve-DnsName and do a little filtering and sorting:

[PS] C:Scripts>$domains | resolve-dnsname -Type MX -Server | where {$_.QueryType -eq "MX"} | Select Name,NameExchange | Sort Name

Name                                                        NameExchange
----                                                        ------------                                                                                       

In the example above I’ve used -Server and one of Google’s DNS server IP addresses to ensure I get the MX record from the public DNS zone and not any internal DNS zones I might be hosting for those domain names.

As you can see it is quite simple to query the MX records for multiple domain names by using PowerShell.

About the Author

Paul Cunningham

Paul is a former Microsoft MVP for Office Apps and Services. He works as a consultant, writer, and trainer specializing in Office 365 and Exchange Server. Paul no longer writes for


  1. Ray Benkart

    Hi Paul,

    I have tried this and I continue to get the error below? I have about 700 domains to query so it would be huge to get this resolved.

    Resolve-DnsName : : DNS name contains an invalid character
    At line:1 char:44
    + $domains = @(Get-Content domainlist.txt) |Resolve-DnsName -Type MX -Server 8.8. …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ResourceUnavailable: ( :String) [Resolve-DnsName], Win32Exception
    + FullyQualifiedErrorId : DNS_ERROR_INVALID_NAME_CHAR,Microsoft.DnsClient.Commands.ResolveDnsName

  2. andre

    Hi Paul!
    Thank you for the free videos. It helps me a lot. I can now perform send and receive email externally but selected domain only (yahoo, outlook live). When I am trying to send email using my test lab to my gmail account, i am receiving undeliverable email rejected by but I can receive email from gmail. Only outgoing email is the issues. I used dynamic IP (dyndns) and our domain hosted by Ipage.
    Meaning, do i need to request to google to allow our IP/Domain? or maybe the issues because of our dynamic IP? Not so sure exactly how to solve this issues. Hope you could help me in this regard. Thank you.

  3. andre

    Thank you. Really appreciated.

  4. andre

    Hi Paul,
    Thank you for your response. Sorry for the confusion. This morning, our domain provider created and pointed already the MX record and route to our local IP.

    But before that, Is there anything i need to do in terms of configuration in the router so i can work the email externally? Right now, we are using the trial version of exchange license. Before we purchase the license we would like to execute first in the test environment. Do i need to purchase the license of exchange 2013 to be able to work in external email?

    Also, I am not so sure if my DNS in local server is working perfectly but our exchange (test environment) is working fine but internally only. If you have any guidelines, free videos for exch 2013 that would really help a lot on my test server. Thank you so much for your kindness.

  5. andre

    Hi Paul,
    I am seeking your help please. I just build a test environment of our exchange server. I used to install Windows Server 2012 and Exchange 2013 in one typical server. I did configure local domain and dns and other pre-req. Now, Exchange is working already locally. Tried also to add the outlook client thru VPN connection and working fine. Further, to be able for us to work with internet and receive email from another domain name, we purchase a domain name in to point the MX in our local Server. They are asking my MX record locally in the server but i did not add any mx record. tried to check thru powershell using this command “Resolve-DnsName -Name -type MX | ft -auto” and it shows “ SOA 3600 Authority 37”. is this my MX record? what are the things that i need to do to be able for me to complete the test environment of my exchange server? your responce in this regard is highly appreciated. thank you so much for your help.

    1. Avatar photo
      Paul Cunningham

      I checked your domain with and it reports that there is an MX record for your domain. I don’t understand the rest of your question.

  6. Richard

    I have 2M domain to check txt records, is there any tool?

  7. Padawan


    Very nice. How can i do to put the results in a file .xls


    Best Regards

  8. Alan

    Hi, I run this on Windows 10 Pro, and seem to end up with no output. Any thoughts as to why? There are nearly 100 domains in the text file, each on its own line with no spaces before or after. See output below:

    PS C:UsersOwnerdesktop> $domains = .domains.txt
    PS C:UsersOwnerdesktop> $domains | Resolve-DnsName -Type MX -Server | where {$_.QueryType -eq “MX”} | Select
    Name,NameExchange | Sort Name
    PS C:UsersOwnerdesktop>

    1. Avatar photo
      Paul Cunningham

      $domains = .domains.txt

      Should be

      $domains = Get-Content .domains.txt

  9. Fateh

    Very nice, thanks.
    After I run these commands, how can I get the results into CSV file ? In other words, how can I get the list of valid / invalid domains ?

  10. Lance

    As always, great info. However…
    The caveat to this is that it’s only supported in newer versions of powershell. I created a script to send an email out when something happens, and it worked fine on our exchange boxes and my machine, but when i put it into productions, it wouldn’t work since the utility server is 2008R2 and has an older version of powershell on it. Just an FYI for anyone who was interested.

  11. Charles Foster

    Good stuff, but the structure of “domains.txt” is? CSV? Difficult for your readers to actually implement with this level of detail. Truly appreciate your efforts.

    1. Avatar photo
      Paul Cunningham

      Just a text file. If it was CSV format I would use .csv for the file name and Import-CSV to ingest it.

Leave a Reply