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.


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!

11 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:

    Domain
    domain1.com
    domain2.com
    domain3.com

    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!

    Like

  2. Hi Peter, I am trying to add 413 domains, but when I run the script I keep getting the errors below. Can you tell me what I may be doing wrong?

    Get-MsolDomainVerificationDns : This domain does not exist. Check the name and
    try again.
    At D:\scripts\import-domains.ps1:4 char:6
    + Get-MsolDomainVerificationDns -DomainName $d.Domain -Mode DnsTxtRecord
    | Se …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [Get-MsolDomainVerificatio
    nDns], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.Domai
    nNotFoundException,Microsoft.Online.Administration.Automation.GetDomainVer
    ificationDns

    New-MsolDomain : A positional parameter cannot be found that accepts argument
    ‘â?Authentication Managed â?Name’.
    At D:\scripts\import-domains.ps1:3 char:6
    + New-MsolDomain â?”Authentication Managed â?”Name $d.Domain
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [New-MsolDomain], Parameter
    BindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.Online.Adm
    inistration.Automation.NewDomain

    Like

  3. Hi Raymond,
    That error indicates that your domains haven’t been added yet, so there’s no way that Get-DomainVerificationDNS is going to work. You’ll need to go back and make sure those domains are added correctly before you can get the DNS verification records.

    Hope this helps!

    Like

    1. Yeah, definitely – the script I’ve provided on this page takes a CSV file where you can list all your domains, and it’ll go through and add them one at a time for you, and then give you the DNS verification record for each domain. Reading through this post again, I realize that I didn’t specify how to create the CSV file which is definitely needed! I’ll update the post to reflect it, but basically, just open Excel (or a text editor) and start a column with “Domain” at the top, and paste all your domains into that list. Import this CSV file in your script and it’ll do the rest.

      Like

  4. Ok. My first issue is I cant even import my domains. I have been trying this command but no luck.

    Import-Csv “C:\scripts\email-domains.csv” | foreach{ New-MsolDomain -Name $_.DomainName }

    Error:
    New-MsolDomain : Invalid syntax. Parameter Name: Name.
    At line:1 char:53
    + … pts\email-domains.csv” |foreach{ New-MsolDomain -Name $_.DomainName }
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [New-MsolDomain], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.StringSyntaxValidationException,Microsoft.Onl

    Like

  5. I ended up manually adding 100 domains and now I am just trying to see if I can Fetch the needed txt record. Would now how to do that? I tried this simple command but I get a pipeline error.

    The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input

    Like

  6. Just to be clear. Here is the script I am trying now.

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

    Failure:

    Get-MsolDomainVerificationDns : This domain does not exist. Check the name and try again.
    At C:\scripts\gettxt.ps1:3 char:6
    + Get-MsolDomainVerificationDns -DomainName $d.Domain -Mode DnsTxt …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (:) [Get-MsolDomainVerificationDns], MicrosoftOnlineException
    + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.DomainNotFoundException,Microsoft.Online.Admi
    nistration.Automation.GetDomainVerificationDns

    “” : The term ‘“”’ is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At C:\scripts\gettxt.ps1:5 char:1
    + “”
    + ~~~~~~
    + CategoryInfo : ObjectNotFound: (“”:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Like

  7. Hi Raymond, definitely… here’s a command I use for that very scenario:

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

    Once you’ve got all your DNS records added, you can work your way through verifying the domains using this command:

    Get-MsolDomain | Where {$_.Status -match “Unverified”} | % {Write-Host “Checking $($_.Name)…”;Confirm-MsolDomain -DomainName $_.Name}

    Let me know if that helps – without seeing your source CSV file, I have no way of troubleshooting the errors you’re running into.

    Like

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.