Clean up Domain Controller DNS Records with Powershell

Summary: Using Windows PowerShell to remove Stale / Dead records.

Q: Hey, Doctor Scripto!

How can I quickly clean up all my dead 's DNS records?


That's a great question. The good Doctor also knows the very person to answer it best. My good friend Patrick Mercier, An PFE who loves working with PowerShell.

Take it away Patrick!

Whether it's as part of Disaster , or because you had an old you needed to get rid of, cleaning up all the records of a now dead DC left behind can be tedious: that is, unless you use PowerShell

So, as an PFE, one of the common things we help customers out with is removing Domain Controllers from the environment. Sometimes that's as simple as the old DC that has to go away or as scary as having recovered AD from backup and having to remove all other DCs as we rebuild. Regardless of the scenario, cleaning DNS is a critical part of this and I've frequently found it to be the part that scares customers the most.

I was cleaning up records manually one day and as I typically do, I thought to myself, there has to be a better way… and there is.

Before I continue though, this is not an Active Directory Disaster article. It's not a DNS clean up article. If you're looking for detailed explanations of all the DNS records this will delete, you'll want to go find an article about Active Directory DNS! What I will do, is demonstrate an easy way to delete all DNS records related to a Domain Controller with a single PowerShell command.

First, let's create an array of all the records in the zone

$dnsrecords = Get-DnsServerResourceRecord -ZoneName “”

This outputs everything in our zone.

What we get though isn't the full picture. The data we need to filter on is part of the “RecordData” data column which in and of itself is an array of data. And to isolate the DC we want to clean up, we'll need to filter the resulting data. For that, we'll filter on some of the attributes available in the RecordData record set, specifically, IPv4Address, NameServer and DomainName.

$deadDC = $dnsrecords | Where-Object {$_.RecordData.IPv4Address -eq “” -or $_.RecordData.NameServer -eq “” -or $_.RecordData.DomainName -eq “”}

Sweet, now I have all the DNS records for my dead Domain Controller in one array!

From here, it's super easy to delete them all, simply by calling the Remove-DnsServerResourceRecord cmdlet against the array and the zone! Because any good domain administrator has a bit of paranoia built in, let's run that as a “What if” to confirm:

$deadDC | Remove-DnsServerResourceRecord -ZoneName “” -whatif

And now, that I've got some peace of mind that nothing I need is being deleted, I simply remove the what if and the records are gone! No manual clean up.

So, if I were to bring all those components into one command, the result is:

Get-DnsServerResourceRecord -ZoneName “” |

Where-Object {$_.RecordData.IPv4Address -eq “”

-or $_.RecordData.NameServer -eq “” -or `

$_.RecordData.DomainName -eq “”} | Remove-DnsServerResourceRecord -ZoneName “” -force

Simple really.

Thanks Patrick for an excellent tip to making all of this happen!

So that is all there is to using PowerShell to cleanup dead Domain Controller records.

I invite you to follow the Scripting Guys on Twitter and Facebook. If you have any questions, send email to them at, or post your questions on the Official Scripting Guys Forum. See you tomorrow.

Until then always remember that with Great PowerShell comes Great Responsibility.

Your Good friend, Dr. Scripto

Windows PowerShell, Patrick Mercier, Scripter

The post Clean up Domain Controller DNS Records with Powershell appeared first on Scripting.


This article was originally published by Microsoft's ITOps Talk Blog. You can find the original article here.