Updating DNS records via PowerShell

Hi All,

I am new to PowerShell, but have written some basic scripts over the years. I mainly work in Unix/Linux so am very familiar with shell, perl, python, etc scripting. I am looking to write some PowerShell scripts to update DNS records, mainly updating IP addresses.

PS Version


In searching around I have found a few examples but so far I have not succeeded. I’m trying to do something simple like this:

Get-DnsServerResourceRecord -ZoneName example.com -RRType A

$new = $old = Get-DnsServerResourceRecord -ZoneName example.com -Name test
$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse('')
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName example.com

Get-DnsServerResourceRecord -ZoneName example.com -RRType A

Which errors out with

HostName                  RecordType Type       Timestamp            TimeToLive      RecordData
--------                  ---------- ----       ---------            ----------      ----------
test                      A          1          0                    01:00:00

Set-DnsServerResourceRecord : Resource record in OldInputObject not found in example.com zone on Server1 server.
At C:\scripts\dnstest.ps1:15 char:1
+ Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Server1:root/Microsoft/...rResourceRecord) [Set-DnsServerResourceRecord], CimException
    + FullyQualifiedErrorId : WIN32 9714,Set-DnsServerResourceRecord

test                      A          1          0                    01:00:00

Can anyone shed some light on how this can be done? Any guidance is greatly appreciated…




$new and $old are references that point to the same object. When you update one, the other is updated. So $old now contains the new IP address when $new is updated and cannot be found. You need to clone the object to have a copy with a difference reference.

$old = Get-DnsServerResourceRecord -ZoneName example.com -Name test
$new = $old.Clone()
$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse('')
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName example.com
@AdminOfThings45 - Thank you for the quick reply, that does the trick.

I am reading that updating the PTR is not automatic. So in general, it seems it would be best to use the block to update IP followed by a block to update the PTR? Is that correct?

Thanks again,


You are correct about the PTR record. By default, when you add a new A record, it will create the PTR record if it can. When performing a change, you will have to do both. I have found that deleting records and then adding new records made that process simpler. It may not make sense for you to do that in every case though.

PowerShell allows you to not only manage your DNS records from the command line but also to take those commands and put them into a script to automate all kinds of time-consuming tasks.

Following are some different queries to update DNS records using powershell

I’ll use the cmdlet to query that DNS zone on the domain controller called DC.

Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local

Now that I can read various DNS records, perhaps I’d like to modify a static record. One of our server names has changed and I need to be sure its DNS record is updated to reflect that.

Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -RRType A

we’ll need to get two identical objects representing a DNS record. In this case, I’m pulling a DNS record for my MySQL server.

$new = $old = Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL

After I have the two objects, I’ll then change the IPV4 address on the new object to represent the IP address it has changed to. Unfortunately, it’s not quite as easy as simply setting a string. The IPV4Address property requires a type of System.Net.IPAddress in order to successfully make the change.

$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse(‘’)
$new.RecordData.IPv4Address = [System.Net.IPAddress]::parse(‘’)
After the IP address is changed on the $new object, I can then use Set-DNSServerResourceRecord to force PowerShell to update the record on the server itself.

Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName mylab.local -ComputerName dc
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName mylab.local -ComputerName dc
Finally, if I’d like to remove the record, the process is much simpler. I can simply pipe the results of Get-DNSServerResourceRecord directly to Remove-DNSServerResourceRecord.

Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL | Remove-DNSServerResourceRecord –ZoneName mylab.local –ComputerName DC
Get-DnsServerResourceRecord -ComputerName dc -ZoneName mylab.local -Name MYSQL | Remove-DNSS

I hope this helps!

Ben Martin