Skip Ribbon Commands
Skip to main content

Quick Launch

Todd Klindt's home page > Todd Klindt's Office 365 Admin Blog > Posts > How to Add a Domain to Office 365 and Azure using PowerShell
January 17
How to Add a Domain to Office 365 and Azure using PowerShell

I love blog posts like this. PowerShell and Office 365, two of my favorite tastes, all rolled up into one. This particular blog post is not groundbreaking, but it’s fun. I’ve recently set up a few Office 365 tenants, and part of that is adding one or more custom domains. Of course you can use the Office 365 Admin Portal to do that, and there’s no shame in that. (Well, maybe a little) But if you want to take your Office 365 Admin game up another level, you can do all of that with PowerShell. I’ll show you how in this blog post.

Getting the Module and the Domain

You need to make sure you have the correct Azure PowerShell module installed before you can take advantage of any of the goodness in this blog post. When I wrote this blog post, I was using version 1.1.166.0 of the MSOnline module. If you don’t have it installed, you can watch Shane’s crappy video walkthrough on installing it. Time marches on, and it marches double-time in the cloud. So while I assume these steps will work later versions of the module, I can’t guarantee that.

To add a domain to your Office 365 tenant, you’ll obviously need a domain. It doesn’t matter where you register the domain, as long as you have the ability to add DNS records. Microsoft does have a deal in place with GoDaddy, so getting your domain there does have some advantages.

Adding the Domain

Once you have the module installed, and the domain purchased it’s time to open up PowerShell and make some magic. Open up “Microsoft Azure Active Directory Module” to get the party started. I christen every PowerShell session with a heaping helping of Start-Transcript, so do that first. Then use Connect-MsolService to connect to your tenant. If you haven’t already added a custom domain to your Office 365 or Azure tenant, then you’re likely logging in with a username that looks like someone@sometenant.onmicrosoft.com, where sometenant is your tenant name, and someone is a tenant admin for that tenant. To get the landscape of the domains in your tenant, use Get-MsolDomain. It looks like this:

SNAGHTML141ce81e 

Once you’ve confirmed the domain you want to add isn’t already there you can use New-MsolDomain to add it. The command I ran was 

New-MsolDomain -Name "toddsblog.com"

It might seem like that should be enough. The domain is added, Ta-Da! If that was the case, this would be a very short blog post. We still have a couple of steps left. I have to prove to Microsoft that I own toddsblog.com. Maybe toddsblog.com is Todd Rundgren’s blog, or maybe Todd Bridges. Who knows? To verify that I am truly the Todd behind toddsblog.com, I have to make a specific DNS entry that Microsoft tells me to add.

Before I can add the correct DNS entry, I need to know what it is. The Get-MsolDomainVerificationDns gives me that value. We have two options, we can create a TXT record, or an MX record. Since I’m not moving the mail for toddsblog.com over to Office 365 right now, I went with the TXT record. If I was moving mail in the short term, I would have went for the MX record instead, and saved myself the step of adding it later. To get the TXT record I need, I ran this command:

Get-MsolDomainVerificationDns -DomainName toddsblog.com -Mode DnsTxtRecord

Here is the output I received:

image 

I used GoDaddy’s domain management tool to add this record. While the label in the screenshot says “toddsblog.com” I really needed to add a record for @, which tells DNS clients “this domain.” It looked like this:

image 

DNS takes its time to move around the Internet, and it gets cached a lot of places along the way. To improve the chances that Microsoft gets the new record, I used nslookup to query a major DNS provider, Google, and make sure it was there:

nslookup -type=TXT toddsblog.com 8.8.8.8

I got the output I expected:

image 

Looks good.

The final step is telling Microsoft to check that we made the entry, and made it correctly. Let’s see what happens:

Confirm-MsolDomain -DomainName toddsblog.com

The output looks like I did it right.

image 

Let’s check our list of domains again with Get-MsolDomain

 

image 

More good news. And finally, in the UI:

image 

The domain shows up as “Setup in progress” because I haven’t added all the DNS records Office 365 needs to send email and stuff like that. The domain is still good though. I can assign that domain to users. It’s fully functional.

To recap, here was the whole process:

New-MsolDomain -Name "toddsblog.com"

Get-MsolDomainVerificationDns -DomainName toddsblog.com -Mode DnsTxtRecord

Confirm-MsolDomain -DomainName toddsblog.com

The other stuff was just fun fluff.

I hope this helps, and encourages you to get your PowerShell on when working with Office 365 and Azure.

tk

ShortURL: http://www.toddklindt.com/PoshAddO365Domain

Edit: Fixed link to MSOnline Module download

Comments

ShortURL broken

 on 3/16/2018 3:34 PM

Re: ShortURL broken

Right you are. Thanks for the heads up. It's fixed now.

tk
Todd O. KlindtNo presence information on 3/22/2018 9:42 AM

Re: How to Add a Domain to Office 365 and Azure using PowerShell

I found this useful.  Thanks for this.
 on 7/8/2019 3:24 PM

Add Comment

Items on this list require content approval. Your submission will not appear in public views until approved by someone with proper rights. More information on content approval.

Title


Body *


Today's date *

Select a date from the calendar.
Please enter today's date so I know you are a real person

Twitter


Want a message when I reply to your comment? Put your Twitter handle here.

Attachments

 

 SysKit