PowerShell: Add Multiple Domains To Office 365 - Master & CmdR
Maybe your like
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.

To start, create a CSV file that has a list of all your domains and save it to your desktop (or wherever you like, just make sure you update your script to reference it). All this CSV file needs is a column header named Domain, and then a list of your domains below it, like so:

or this, if you prefer using Notepad: 
Use this CSV file to run the script above, and when it finishes, you’ll have a CSV output file (which will save to the current directory you’re in), and 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!
Share this:
- X
Related
Tag » Add Domain O365 Powershell
-
Add A Domain To A Client Tenancy With Windows PowerShell For DAP ...
-
New-MsolDomain (MSOnline) - Microsoft Docs
-
Adding A Domain To Office 365 With PowerShell - MSB365
-
Add Office 365 Domain Using PowerShell - ThatLazyAdmin
-
Adding Domains In Bulk Through PowerShell - Setting Up And ...
-
Manage Domains In Office 365 Using PowerShell | Jaap Wesselius
-
How To Add A Custom Domain To Office 365 Using PowerShell
-
How To Add A Domain To Office 365 And Azure Using PowerShell
-
How To Find Your Primary (Default) Office 365 Domain Name
-
How To Set A Domain Email Alias For Office 365 Users On A Specific ...
-
Add Your Domain Name To Trusted Senders For All Delegated Office ...
-
How To Sync An Existing Office365 Tenant Into A New Active ...
-
Adding Secondary Domain To All Users 365, Bulk, Powershell?
-
Add Domain To Office 365 Tenant - ALI TAJRAN