Microsoft DNS record updates using PowerShell and DNSCMD



Hi folks! some of the comments about records that are listed with (same as parent folder) as the name caused me to make an update. I’ve noted the updates in the following post:

I have also updated the code below to reflect the new code.


There are occasions where we need to do bulk DNS record management such as create and update a large series of records for IP network changes or BCP testing. The environment I am working with is a Microsoft DNS zone. It can be a standard Primary, or an Active Directory Integrated zone. Either type will work with the process we are creating here.

This is not a typical and common process as DNS is often self-managing and dynamic. Let’s assume that you have a number of records from devices that cannot dynamically update, and you are currently using static A and CNAME records to define them.

Because this is not a commonly occurring task, there are no native PowerShell CmdLets to do this. What we can do however is get the best of breed by using PowerShell to parse a file and craft the command line to use the Microsoft DNSCMD command line tool.

This process assumes that your records are all located in the root if the zone you define with the $DNSZone variable. The idea is that you can take this concept and flavour to taste for your particular needs.

The zone that we are managing for our example script is shown here:

To update the zone, we have to use a CSV file which is located in the same directory as the script, or you can define the full drive letter and path with the $InputFile variable if you so desire. The CSV file requires a header row which is name,type,address to give us our attributes for each object when we parse the file.

The script logic is fairly simple:

  1. Define our zone info (server and zone name)
  2. Import the CSV file into an array
  3. Loop through the array
  4. Create a delete command line and use the Invoke-Expression CmdLet to execute it
  5. Create an add command line and use the Invoke-Expression CmdLet to execute it

I haven’t added any fancy logging or anything, but there is some Write-Host CmdLets used to output the commands to screen so that you can ensure that they are coming out as expected.

Fair warning on this one. Because we are using the Invoke-Expression CmdLet to launch the DNSCMD executable, we don’t have the luxury of a -WhatIf option. You can comment out the Invoke-Expression lines and run using just the Write-Host output as a sanity check on your structure. And by “can” I mean you very definitely should!

The script can be downloaded at my TechNet Gallery here:

Here is the script:

# Environment Setup
$DNSServer = “DC1”
$DNSZone = “”
$InputFile = “dnsrecords.csv”

# Read the input file which is formatted as name,type,address with a header row
$records = Import-CSV $InputFile

# Now we loop through the file to delete and re-create records
# DNSCMD does not have a modify option so we must use /RecordDelete first followed by a /RecordAdd

ForEach ($record in $records) {

# Capture the record contents as variables
$recordName = $
$recordType = $record.type
$recordAddress = $record.address

# Build our DNSCMD DELETE command syntax
$cmdDelete = “dnscmd $DNSServer /RecordDelete $DNSZone $recordName $recordType $recordAddress /f”

# Build our DNSCMD ADD command syntax
$cmdAdd = “dnscmd $DNSServer /RecordAdd $DNSZone $recordName $recordType $recordAddress”

# Now we execute the command
Write-Host “Running the following command: $cmdDelete”
Invoke-Expression $cmdDelete

Write-Host “Running the following command: $cmdAdd”
Invoke-Expression $cmdAdd

So let’s run the script and see the results. This is the output from the script:

And now the resulting zone configuration shows the new updated records as per our CSV file:

While this may be a very simple example, it is meant to be a starting point to show you how to make the best of a situation where there is not a native PowerShell CmdLet. Being able to use hybrid scripting tools and techniques is a great thing to have in your toolkit of skills.

I hope that you find this useful!


Comments 18

    1. Post

      Hi Lakend,

      You sure can! All that you need to do is adjust the DNSCMD parameters for the TYPE field. Because we pass this into the command through PowerShell Parameters read from the file ($recordType) you just change the line in your text file to have the type column as PTR.

      That should do all you need to do. Let me know if that works 🙂


    1. Post

      Hi Adnan,

      For multiple zones there are a few ways to tackle it. You could also add a zone field into the CSV and modify the update process, or another way would be to handle the zone statically and loop through the update process. For cleanliness the CSV option is ideal.

      Sounds like a good chance for a part 2 🙂


  1. Is there a way we can add entries for the parent domain? In the GUI, if you leave the A record host blank, it defaults to ‘(same as parent)’. I have tried leaving this field blank in the CSV and entering ‘(same as parent)’. Neither of these worked.

    P.S. Thanks for the script, very useful.

    1. Post
  2. Hi Eric,

    Thanks for the great script. This will save me a lot of work.
    I would like to know is it possible to replace and add blank records?

    I have a two (same as parent folder) Blank records. One of them is Host (A) record and the other one is TXT (for SPF usage) record.

    I created a csv file like this;

    zone,name,type,address,www,A,111.222.333.444,mail,A,111.222.333.444,ftp,A,111.222.333.444,,A,111.222.333.444,,TXT,v=spf1 a mx ip4:111.222.333.444 -all

    But when I run the script it updates records except the name field left blank.

    How do I update these blank ones?

    Thank you.

    1. Post
  3. Pingback: Updating (same as parent folder) records with DNSCMD and PowerShell | DiscoPosse – Using the chicken to measure IT

  4. Can this be modified without CSV? i want to create 2 scripts to change a dns entry. if a DR situation occurs hit script 1 to change the entry to go to this IP then Script 2 changes it all back

    1. Post

      Hi Chris,

      You could write the entries explicitly in the script with some code changes, but the ideal is to always separate the input from the code so that you can reuse the same script file.

      For your situation, you can just create two scripts called DR-DNS.ps1 and PROD-DNS.ps1 and then set the input file ($InputFile variable) in each file to point to the appropriate CSV file with the information in it.

      Let me know if that works for you.

      Thanks! Eric

  5. Hi,

    I’m looking at your screenshot for the results of running the script. Every time you execute a command it prints 2 rows to the screen stating the action that has been performed and that the command was executed successfully. Where does these 2 lines come from? Is it possible to capture them in a variable ?

    I’m trying to write a similar script but without access to DNS, which makes testing a little tricky.

    1. Post

      Hi Hannah,

      Those come from the standard output of the CmdLet. You can pipe wrap a result checker around it potentially depending on how you want to handle the result and pass it to other commands.

      Is there a particular next step you want to trigger based on the result?


  6. Hi,

    Thanks for your reply. I’ll give it a shot.
    I’ve been asked to capture any success/failure message from the CmdLet and write it to the Event Logs along with some other information.


  7. Hi,

    I’m having challenges with the script. a new record is created but the old record still exist in dns intead of deleting the old one.
    How do i rectify this?


    1. Post

      Hi Buba,

      Which version of Active Directory and which server edition are you running? I may need to revisit this with more recent versions as I haven’t updated content in quite a while for some of these older scripts.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.