Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

 Windows OS Hub / PowerShell / Create & Manage DNS Zones and Records with PowerShell

April 3, 2023 PowerShellWindows Server 2016

Create & Manage DNS Zones and Records with PowerShell

A Windows administrator can use the good old Dnscmd cli tool or DNSServer module for PowerShell to manage DNS zones and records. In this article we’ll cover the basic operations of bulk creating, modification, and removing different DNS records or zones using PowerShell.

Contents:
  • DNSServer PowerShell Module
  • Manage DNS Zones with PowerShell
  • Managing DNS Records with DNSServer PowerShell Module
  • How to Create Multiple A and PTR DNS Records from a .CSV File?

DNSServer PowerShell Module

The DNSServer module for PowerShell is a part of RSAT. On Windows 10 you will have to install RSAT separately, and on Windows Server you can enable the module using Server Manager GUI (Role Administration Tools -> DNS Server Tools).

install DNS Server Tools with DNSServer module for PowerShell

Make sure the DNSServer PowerShell module is install on your computer:

Get-Module DNSServer –ListAvailable

You can display the list of commands in it (the module version for Windows Server 2016 has 134 cmdlets):

Get-Module DNSServer

Get-Module DNSServer

Manage DNS Zones with PowerShell

Display the list of DNS zones on your server (in our case, it is a domain controller):

Get-DnsServerZone –ComputerName dc01

To add a new primary DNS zone named woshub.com, run this command:

Add-DnsServerPrimaryZone -Name woshub.com -ReplicationScope "Forest" –PassThru

As you can see, the primary DNS zone integrated into Active Directory has been created (isDsIntegrated=True).

Add-DnsServerPrimaryZone

You can create a Reverse Lookup Zone:

Add-DnsServerPrimaryZone -NetworkId "192.168.100.0/24" -ReplicationScope Domain

To synchronize a new zone with other DCs in the domain, run the following command:

Sync-DnsServerZone –passthru

Display the list of records in the new DNS zone (it is empty):

Get-DnsServerResourceRecord -ComputerName dc01 -ZoneName contoso.local

Get-DnsServerResourceRecord

To remove the DNS zone, use the command:

Remove-DnsServerZone -Name woshub.com -ComputerName dc01

It will also remove all existing DNS records in the zone.

Managing DNS Records with DNSServer PowerShell Module

To create a new A record for the host in the specified DNS zone, use this command:

Add-DnsServerResourceRecordA -Name ber-rds1 -IPv4Address 192.168.100.33 -ZoneName woshub.com -TimeToLive 01:00:00

To add a PTR record to the Reverse Lookup Zone, you can add –CreatePtr parameter to the previous command or create the pointer manually using the Add-DNSServerResourceRecordPTR cmdlet:

Add-DNSServerResourceRecordPTR -ZoneName 100.168.192.in-addr.arpa -Name 33 -PTRDomainName ber-rds1.woshub.com

To add an alias (CNAME) for the specific A record, run this command:

Add-DnsServerResourceRecordCName -ZoneName woshub.com -Name Ber-RDSFarm -HostNameAlias ber-rds1.woshub.com

To change (update) the IP address in the A record, you will have to apply quite a complex method since you cannot change an IP address of a DNS record directly:

$NewADNS = get-DnsServerResourceRecord -Name ber-rds1 -ZoneName woshub.com -ComputerName dc01
$OldADNS = get-DnsServerResourceRecord -Name ber-rds1 -ZoneName woshub.com -ComputerName dc01

Then change the IPV4Address property of the $NewADNS object:

$NewADNS.RecordData.IPv4Address = [System.Net.IPAddress]::parse('192.168.100.133')

Change the IP address of the A record using the Set-DnsServerResourceRecord cmdlet:

Set-DnsServerResourceRecord -NewInputObject $NewADNS -OldInputObject $OldADNS -ZoneName woshub.com -ComputerName dc01

Make sure that the IP address of the A record has changed:

Get-DnsServerResourceRecord -Name ber-rds1 -ZoneName woshub.com

Change/Update DNS Host Record IP Address via PowerShell

You can display the list of DNS records of the same type by using the –RRType parameter. Let’s display the list of CNAME records in the specified DNS zone:

Get-DnsServerResourceRecord -ComputerName DC01 -ZoneName woshub.com -RRType CNAME

Get-DnsServerResourceRecord RRType

You can also use filters by any DNS record parameters using Where-Object. For example, to display the list of A records containing rds phrase in their hostnames:

Get-DnsServerResourceRecord -ZoneName woshub.com -RRType A | Where-Object HostName -like "*rds*"

Get-DnsServerResourceRecord Where-Object HostName like

To remove DNS records, the Remove-DnsServerResourceRecord cmdlet is used.

For example, to remove a CNAME record, run the command:

Remove-DnsServerResourceRecord -ZoneName woshub.local -RRType CName -Name Ber-RDSFarm

To remove an A DNS record:

Remove-DnsServerResourceRecord -ZoneName woshub.local -RRType A -Name ber-rds1 –Force

To remove a PTR record from a Reverse Lookup Zone:

Remove-DnsServerResourceRecord -ZoneName “100.168.192.in-addr.arpa” -RRType “PTR” -Name “33”

How to Create Multiple A and PTR DNS Records from a .CSV File?

Suppose, you want to create multiple A records at a time in the specific DNS Forward Lookup Zone. You can add them one-by-one using the Add-DnsServerResourceRecordA cmdlet, but it is easier to add A records in bulk from a .CSV file.

Create a text file NewDnsRecords.txt with the names and IP addresses you want to add to DNS. The txt file format is as follows:

HostName, IPAddress

Adding Multiple DNS Records From .TXT/ .CSV File with PowerShell Script

To create A records in the woshub.com zone according to the data in your TXT/CSV file, use the following PowerShell script:

Import-CSV "C:\PS\NewDnsRecords.txt" | %{
Add-DNSServerResourceRecordA -ZoneName woshub.com -Name $_."HostName" -IPv4Address $_."IPAddress"
}

If you want to add records to the Reverse Lookup Zone at the same time, add the –CreatePtr parameter to your Add-DNSServerResourceRecordA command.

Then using DNS Manager console (dnsmgmt.msc) or Get-DnsServerResourceRecord -ZoneName woshub.local make sure that all DNS records have been created successfully.

Bulk add DNS recordes using PoweShell

If you want to add PTR records to the Reverse Lookup Zone in bulk, create a text or a CSV file with the following structure:

octet,hostName,zoneName
102,ber-rds2.woshub.com,100.168.192.in-addr.arpa
103,ber-rds3.woshub.com,100.168.192.in-addr.arpa
104,ber-rds4.woshub.com,100.168.192.in-addr.arpa
105,ber-rds5.woshub.com,100.168.192.in-addr.arpa

Then run the script:

Import-CSV "C:\PS\NewDnsPTRRecords.txt" | %{
Add-DNSServerResourceRecordPTR -ZoneName $_."zoneName" -Name $_."octet" -PTRDomainName $_."hostName"
}

Make sure that your PTR records appeared in the DNS Reverse Lookup Zone.

3 comments
2
Facebook Twitter Google + Pinterest
previous post
Office 2019 Deployment Guide for Enterprise Using Deployment Tool
next post
Control Panel Mail App Not Found on Windows 10

Related Reading

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

October 15, 2023

How to Query and Change Teams User Presence...

October 8, 2023

Installing Language Pack in Windows 10/11 with PowerShell

September 15, 2023

3 comments

Katie June 19, 2020 - 1:37 pm

Thank you for this post! Very awsome and very helpful!

Reply
Learoy Ellis-Moore February 5, 2021 - 9:36 pm

You are an OG baller, thanks so much i have been playing wither server core and trying to create a AD using powershell however NSLOOKUP return no server name as i had no reverse lookup so need to find info on how to create a reverse dns entry

Reply
Dhananjay October 6, 2021 - 3:36 pm

Thank you so much, it works well but you need to add -computername switch at the end of the command (in case you are running this command from terminal server and not DNS/DC)

Reply

Leave a Comment Cancel Reply

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023
  • How to Use Ansible to Manage Windows Machines

    September 25, 2023
  • Installing Language Pack in Windows 10/11 with PowerShell

    September 15, 2023
  • Configure Email Forwarding for Mailbox on Exchange Server/Microsoft 365

    September 14, 2023
  • How to View and Change BIOS (UEFI) Settings with PowerShell

    September 13, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11
  • PowerShell: Get Folder Sizes on Disk in Windows
  • Managing Printers and Drivers with PowerShell in Windows 10 / Server 2016
  • Deploy PowerShell Active Directory Module without Installing RSAT
  • Managing User Photos in Active Directory Using ThumbnailPhoto Attribute
  • Managing Saved Passwords Using Windows Credential Manager
Footer Logo

@2014 - 2023 - Windows OS Hub. All about operating systems for sysadmins


Back To Top