Use PowerShell 3.0 to Clear the Client DNS Cache on Workstations

Doctor Scripto

Summary: Microsoft Scripting Guy, Ed Wilson, shows how to use Windows PowerShell 3.0 to clear the client DNS cache on workstations. Microsoft Scripting Guy, Ed Wilson, is here. One of the things I love about Windows PowerShell 3.0 in Windows 8 is the number of cmdlets available. These cmdlets make it possible to perform literally hundreds of tasks from within Windows PowerShell. Thus, I rarely leave Windows PowerShell, go to Start, and type to bring up whatever GUI tool I might have needed in times past. To be perfectly honest, I never used the GUI to clear the DNS cache on my local computer; nor did I ever use the GUI to register that client in DNS. The cool thing is that there are now cmdlets to perform this rather pedestrian task. In the past, I used ipconfig to flush the DNS cache and to register my computer with DNS. Of course, when switches were added to ipconfig, I always wondered why exactly they belong with ipconfig, other than the fact that it was a pretty handy place to add the functionality. Of course trying to talk a NOOB through this process on the phone is always a challenge. The two commands, flushing the DNS cache and registering the computer in DNS, do not have to go together. The first command removes entries from the DNS resolver cache, and therefore forces the computer to retrieve up-to-date information from DNS. I run this command at least once a week—for example, when I wake up early in the morning and check to ensure that the day’s Hey, Scripting Guy! Blog posted on time and correctly. At times I go to the blog, and the new entry does not appear to have posted. Rather than getting all in a tizzy, I have learned that for some reason, cleaning out old entries in my DNS cache resolves the issue. In the past, I used ipconfig /flushdns to perform that task. I also figure that as long as things need updating, I may as well ensure that my computer information is registerd properly in DNS. This is a preventive action, rather than a corrective task.

Flushing DNS cache and registering DNS across the network

Because flushing the DNS cache and registering DNS clients is a task that I perform on a routine basis, I decided to take a couple of minutes to knock out a script to perform this action. The first thing I do is import the Active Directory module. Then I use the Get-ADComputer cmdlet to find the name of all computers on the network running Windows 8. These three lines of code are shown here. Import-Module activedirectory

$c = Get-ADComputer -Filter * -Properties operatingsystem |

   where operatingsystem -match 8 Now I use the Invoke-Command cmdlet to run the Clear-DNSClientCache and the Register-DNSClient cmdlets on remote machines running Windows 8. This command is shown here.

Invoke-Command -cn $c.name -SCRIPT {

  Clear-DnsClientCache

  Register-DnsClient } Here is the complete script:

ClearDNSCacheAndRegister.ps1

Import-Module activedirectory

$c = Get-ADComputer -Filter * -Properties operatingsystem |

   where operatingsystem -match 8

Invoke-Command -cn $c.name -SCRIPT {

  Clear-DnsClientCache

  Register-DnsClient } Well, that is about all there is to using Windows PowerShell 3.0 and Windows 8 to flush the DNS cache and to register the client in DNS. More cool Windows PowerShell stuff will continue tomorrow. I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace. Ed Wilson, Microsoft Scripting Guy 

0 comments

Discussion is closed.

Feedback usabilla icon