PowerShell: Add Multiple Domains to Office 365

Usually, adding email domains to Office 365 is a pretty straightforward affair, and I usually just add them through the Office 365 admin portal. However, if you need to add multiple domains into Office 365, clicking through them one at a time is a painful process – I was recently working on a project that need 70 email domains added, and the results of that need is the PowerShell script below:

$domains = Import-Csv .\email-domains.csv
foreach ($d in $domains){
     New-MsolDomain –Authentication Managed –Name $d.Domain
     Get-MsolDomainVerificationDns -DomainName $d.Domain -Mode DnsTxtRecord | Select Label,Text,ttl | Export-Csv .\domain-verification.csv -NoTypeInformation -Append
Write-Host “Domains added to Office 365, fetching DNS verification records now”

This little block of code will iterate through your list of domains and add them to Office 365 one at a time, and then fetch the verification txt records for you to go back and add on your DNS host.

If you look for your CSV output file (default is the current directory you’re in), you’ll see all your domains and DNS records nicely listed for you:

And of course, your domains are in Office 365, ready for the next steps:

After you’ve added your DNS records, use this next line to iterate through your unverified domains and confirm them:

Get-MsolDomain | Where {$_.Status -match “Unverified”} | % {Confirm-MsolDomain -DomainName $_.Name}

And if you need to check how many domains are left that need to be verified:

(Get-MsolDomain | Where {$_.Status -match “Unverified”}).count

I’m adding this line because I needed to fetch all the DNS verification records again:

Get-MsolDomain | Where {$_.Status -match “Unverified”} | % {Get-MsolDomainVerificationDns -DomainName $_.Name -Mode DnsTxtRecord} | Select Label,Text,Ttl | Export-Csv .domain-verification.csv -NoTypeInformation

You can also modify this command to remove all your unverified domains and start over if necessary:

Get-MsolDomain | Where {$_.Status -match “Unverified”} | % {Remove-MsolDomain -DomainName $_.Name -Force}

Definitely a useful bit of code – adding domains one by one is fine if you only have a handful to do, but this will allow you to grab a whole list of domains and import them all in one shot; even better, once you (or whomever manages your DNS for you) have added your DNS entries, you can use this cmdlet to find whichever ones are still unverified and recheck them whenever you feel like – which in my case, usually ends up being every few minutes 😉

Hope this helps – check my other PowerShell posts for other useful tips and tricks!


2 thoughts on “PowerShell: Add Multiple Domains to Office 365

  1. Hey Peter, your CSV file would only need to have Domain as a header – for example:


    That way when the file is imported, $d.Domain will return one of those domains per line.

    Let me know if that works for you, or if you have any other questions – thanks!


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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.