Adding multiple domains to Office 365 in powershell

There may be times when you need to add tens or even hundreds of domains to an Office 365 tenant so using the GUI isn't really an option.

I've created a little script here that will make this process easier.

It takes a csv file (called domains.csv) that exists in the same folder as the script itself and outputs the domain proof required for each domain to a domains.log text file.

The CSV file must be a single column csv file in the following format;

Domain
domain1.co.uk
domain2.co.uk

I've included a generic domain CSV file in the downloadable zip archive if that's easier for you.

write-host
write-host --------------------------------------------
write-host Authenticating....
write-host --------------------------------------------
Import-Module MSOnline
$cred = Get-Credential
Connect-MsolService -Credential $cred
$count=import-csv .\domains.csv | measure
$domainlist=import-csv .\domains.csv
write-host
write-host --------------------------------------------
write-host Importing $count.count Domains....
write-host --------------------------------------------
$logfile=".\domains.log"
Function LogWrite {
Param ([string]$logstring)
Add-content $logfile -value $logstring
}
logwrite "Domain,TXTRecord"
foreach ($domain in $domainlist)
{
New-MSOLDomain -name $domain.domain
$proof = (Get-MSOLDomainVerificationDNS -domainname $domain.domain | select-object -expandproperty label).split(".")[0]
$txtrecord="MS=" + $proof
$domainrecord=$domain.domain
logwrite "$domainrecord,$txtrecord"
}

You can download the script and sample domains.csv file here

Tony currently works at Cloudworks implementing Office 365 and Azure based projects.