PowerShell DNS KB ID 0001906
Problem
You can use PowerShell to create DNS records in a Windows environment, this is typically on a Windows Server OS, that is running DNS services. Below are some methods (depending on whether you’re managing an Active Directory-integrated DNS zone or a standalone DNS server) to do this.
Solution
PowerShell DNS: Creating an A Record (Host Record)
To add an A record (IPv4 address) to a DNS zone, use the following syntax.
[box]
Add-DnsServerResourceRecordA -Name "my-server" -ZoneName "pnl.com" -IPv4Address "192.168.100.101"
[/box]
PowerShell DNS: Creating an AAAA Record (IPv6 Host Record)
To add an A record (IPv4 address) to a DNS zone, use the following syntax.
[box]
Add-DnsServerResourceRecordAAAA -Name "my-server" -ZoneName "pnl.com" -IPv6Address "2001:db8::2"
[/box]
PowerShell DNS: Creating an CNAME Record (Cananonical Name Record)
To add an CNAME record (IPv4 address) to a DNS zone, use the following syntax.
[box]
Add-DnsServerResourceRecordCName -Name "www" -ZoneName "pnl.com" -HostNameAlias "my-server.pnl.com"
[/box]
PowerShell DNS: Creating an MX Record (Mail Exchange)
To add an MX record for mail delivery to a DNS zone, use the following syntax, Note: This will create an MX record with a priority of 10.
[box]
Add-DnsServerResourceRecordMX -ZoneName "pnl.com" -Name "@" -MailExchange "mail-01.pnl.com" -Preference 10
[/box]
PowerShell DNS: Creating an PTR Record (Reverse DNS)
To add an PTR record (or a reverse DNS lookup record) the following syntax, Note: Assumes you already have a reverse lookup zones created.
[box]
Add-DnsServerResourceRecordPTR -Name "101" -ZoneName "100.168.192.in-addr.arpa" -PtrDomainName "my-server.pnl.com"
[/box]
Note: This creates a PTR Record for 192.168.100.101 that points back to my-server.pnl.com.
PowerShell DNS: Creating an TXT Record (For SPF, DKIM, etc)
To add an TXT record use the following syntax.
[box]
Add-DnsServerResourceRecord -DescriptiveText "v=spf1 mx -all" -Name text -Txt -ZoneName pnl.com
[/box]
Using PowerShell for DNS record creation helps streamline the administrative workload, improves consistency, and reduces the potential for errors, especially in larger environments. It’s an efficient, flexible, and scalable solution that can significantly enhance the management and automation of DNS infrastructure. Whether for one-off record creation or bulk updates, PowerShell is a powerful tool for IT administrators.
Related Articles, References, Credits, or External Links
Setting up the Correct DNS Records for your Web or Mail Server