Clear DNS Server Cache

Clear the DNS server cache on all domain controllers.

How often have you needed to clear the DNS server cache?

Regardless how many domain controllers in your environment, a few simple lines of PowerShell can clear the cache on all of them.

First, we need to get the name of the domain. Of course, if you already know the name, you can simply use it.

1
$DomainName = Get-ADDomain | Select-Object -ExpandProperty DnsRoot

Next, we find all name servers in the domain and show only the hostname.

1
$DomainControllers = (Resolve-DnsName -Name $DomainName -Type NS).Where({ $_.Type -eq 'NS' }).NameHost

Lastly, we execute the clear command for each one.

1
$DomainControllers.ForEach({ Clear-DnsServerCache -ComputerName $_ -Force })

Of course, there are a few shortcuts you can do to get this to a one-liner.

1
(Resolve-DnsName -Name (Get-ADDomain | Select-Object -ExpandProperty DnsRoot -Type NS).Where({ $_.Type -eq 'NS' }).NameHost.ForEach({ Clear-DnsServerCache -ComputerName $_ -Force })

For more information, please see the following Microsoft docs:

Have a great day!

This site uses Google Analytics with privacy-focused settings: • IP addresses are anonymized • Do Not Track signals are honored • Cookies are used to track sessions

This work is licensed under CC BY-NC-SA 4.0 CC BY NC SA


Built with Hugo
Theme Stack designed by Jimmy