DNS Creation in PowerShell
Learn how to create DNS records using PowerShell.
March 6, 2015
Q. How can I create a DNS record in PowerShell?
A. To create DNS host records prior to Windows Server 2012 R2, Windows 8.1 and PowerShell v4 it was necessary to use the CreateInstanceFromTextRepresentation of the MicrosoftDNS_ResourceRecord WMI class which is documented at https://msdn.microsoft.com/en-us/library/windows/desktop/ms682714(v=vs.85).aspx and an example usage is shown below:
$dnsServer = "savdaldc01.savilltech.net"$dnsZone = "savilltech.net"$hostA = "test.savilltech.net"# Set the class: 1 = Internet, 2 = CSNet, 3 = CHAOS, 4 = Hesiod$recordClass = 1$ttl = 3600$IPAddress = "10.7.173.25"#A type record$dnsAType = [wmiclass]"rootMicrosoftDNS:MicrosoftDNS_AType"# Use CreateInstanceFromPropertyData to create the host A record$dnsAType.CreateInstanceFromPropertyData($dnsServer, $dnsZone, $hostA, $recordClass, $ttl, $IPAddress)
With PowerShell v4 and above it is much simpler. The Add-DnsServerResourceRecordA cmdlet can be used (there are other cmdlets for other types of record as documented at https://technet.microsoft.com/en-us/library/jj590772.aspx). The same result as the script above can be achieved using only the following:
Add-DnsServerResourceRecordA -IPv4Address 10.7.173.25 -Name test -ZoneName savilltech.net -TimeToLive 3600
About the Author
You May Also Like