This article was originally posted on the blog.  

Manage DNS Zones With PowerShellIf you find yourself making changes on your Microsoft DNS servers using the DNS MMC snapin you’re probably wasting a lot of time.

Why? Because it’s possible to create, modify or remove any DNS object that you can from the MMC with PowerShell! By using PowerShell to manage DNS allows you not only control things from the command line but to take those commands and put them into a script to automate all kinds of time-consuming tasks.

To limit this article’s scope, we’re going to just focus on managing DNS zones with PowerShell although it’s completely possible to administer other DNS objects like records and the server itself as well.

Don't just learn from my blog and keep that newfound knowledge to yourself! Share it! I've created a new service called TechSnips that's dedicated to helping technology professionals share their knowledge and look awesome in the process! Through small screencast videos, full courses and written content, TechSnips is a community to help you grow your career. Join us!.

Before we get too far, there are a few prerequisites you need to be aware of. First, I’m assuming you have permissions to read, modify and remove DNS zones from your Windows DNS servers. Second, I’ll be demonstrating a few concepts from DNS servers that are in an Active Directory domain with AD-integrated zones. PowerShell is still capable of managing zones and records outside of Active Directory but may not quite be the same result as I’ll be showing you here. Finally, you’ll need to ensure you have a version of the Remote Server Administration Tools (RSAT) installed on your client specific to your operating system.

Now that we have that out of the way let’s start out by first ensuring the DNSServer module is available to us. To do that, I’ll use the Get-Module cmdlet.

First up, let’s create a zone with PowerShell. To do this, we’ll use the Add-DnsServerPrimaryZone function. The simplest way this can be done is by using two parameters. Those parameters are Name and ReplicationScope. However, in our example, I’ll also be using the ComputerName parameter since I’m invoking this command on a remote computer.

Above you can see that my domain is mylab.local and my zone name is testzone. My DNS server is DC so I’m specifying that for the ComputerName parameter and finally since this server is on my domain I have to also set the ReplicationScope so I’ve chosen to replicate this zone amongst all other DNS servers in my Active Directory forest.

Next, to verify this zone was created, I can then use the Get-DnsServerZone command. I could use the Name parameter but to show you all of the zones I have I’ll just tell Get-DnsServerZone to find all of them.

And just to be sure we go through the entire lifecycle of a DNS zone, I’ll then remove it.

This will also remove all the records in the zone, and the server will no longer host the zone, do you want to continue?

There is so much more possible with managing DNS zones in PowerShell. I encourage you to look through all of the commands possible in Get-Command -Module DnsServer -Noun Zone. This command gives you a list of all of the commands inside of the DnsServer module that have ‘Zone’ in the name. You’ll find that the command names are self-explanatory and if you need to investigate further always consult the help of each command using Get-Help.

%d bloggers like this: