Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We've been moving data centers and I have a lot of old records that were not correctly but in DNS as CNAME records, but A records that have a direct IP (e.g. 192.168.100.n) and they're all moving to a new subnet (10.19.100.n).

I just need to write a powershell script to change all those records. I found this site:

and from it I made this simple script:

$dnsServer = "meldc2"

$scope = New-Object Management.ManagementScope("\\$dnsServer\root\MicrosoftDNS")
$path = New-Object Management.ManagementPath("MicrosoftDNS_Zone")
$options = New-Object Management.ObjectGetOptions($Null,[System.TimeSpan]::MaxValue, $True)
$ZoneClass= New-Object Management.ManagementClass($scope,$path,$options)
$Zones = Get-WMIObject -Computer $dnsServer -Namespace "root\MicrosoftDNS" -Class "MicrosoftDNS_Zone"
$Zones | Get-Member
foreach($Z in $Zones) {
  $Z | Select-Object Name,DsIntegrated,ZoneType,Reverse,Data

but that only gets me a listing of root zones. I don't understand how to iterate over all the entries in each of the zones. Also, all the examples I've seen involve adding new zones, but there aren't any examples I can find on modifying existing A records.

share|improve this question
up vote 4 down vote accepted

I would use dnscmd for modifying DNS records:

dnscmd /recorddelete A
dnscmd /recordadd host A 10.19.100.n

This can be wrapped in a loop in either batch or PowerShell, e.g. like this:

$domain = ""
dnscmd /enumrecords $domain `@ /type A | % {
  $name = $_.split(" ")[0]
  $ip = $_.split("`t")[-1] -replace "192.168.100", "10.19.100"
  dnscmd /recorddelete $domain A "$name.$domain"
  dnscmd /recordadd $domain $name A $ip
share|improve this answer
I really need to iterate over the individual entries in each zone since each zone has a lot of subdomains. I need to specifically check to see if the IP matches the IP I need to chance and flip that particular entry. – djsumdog Oct 6 '12 at 20:52
Use /enumrecords to enumerate the records of the zone. See updated answer. – Ansgar Wiechers Oct 6 '12 at 21:29
Thanks! I added my full solution to yours and an explanation is available on my site:… – djsumdog Oct 8 '12 at 1:34
One suggestion concerning your address check: rather than doing a boatload of $ip.Contains() in the if condition I'd create an array $ip_list = @("", "", ...) and change the condition to if ($ip_list -contains $ip). – Ansgar Wiechers Oct 8 '12 at 8:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.