Use PowerShell 3.0 to Clear the Client DNS Cache on Workstations

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 $ -SCRIPT {


  Register-DnsClient }

Here is the complete script:


Import-Module activedirectory

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

   where operatingsystem -match 8

Invoke-Command -cn $ -SCRIPT {


  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, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Comments (9)

  1. I am anxious to use the powershell v3 on my servers

  2. Walid Toumi says:

    Hi Ed,

    —> it's a great idea

    "where operatingsystem -match 8"

    —-> but if OS is not W8 it dont't work, use  "where {$_.operatingsystem -match 8}" is more secure

    (sorry for my english)

  3. Ed Wilson says:

    @Walid Toumi also keep in mind that simple pattern would also MATCH 2008 AND 2008 R2. Therefore a better pattern is required to match only WIndows 8 … but it all depends on your network what you can get away with using. I am glad you liked the idea … Thanks.

  4. Ed Wilson says:

    @Matheus Kamphorst  yes, PowerShell 3,on the server … the stuff dreams are made of. It is installed by default on WIndows Server 2012. But it will "downlevel" to Windows Server 2008 R2.

  5. Leo Jacob says:

    Hang on a sec, putting "ipconfig /flushdns" and/or "ipconfig /registerdns" in a text file and saving it as a cmd or bat files is more tedious?  Im not exactly clear on what the point is here.  Use an easy command you are used to, or have to learn powershell.  If you have worked in powershell very long you know that based on configuration it isnt always this simple.  Then you have to find out why the script wont run, fix it, test it and do more research.  Whatever happened to K.I.S.S (keep it simple stupid)  

    To be honest, I like powershell, emphasis on LIKE.  I dont love it.  I write in C# and it took me quite a while to learn it and to be honest aside from the cmdlets, powershell isnt much different.  Although more powerful than other languages, it is by far not simpler.

    I see where this script would be beneficial to admins though, I would prefer to use a good old batch instead, but Powershell over VBS anyday

  6. dnsclient on windows 7 says:

    I've looked quite a bit and can't find a reference to how to get the dnsclient module on windows 7. I'm using powershell 3 – is there a reason I wouldn't be able to use the dnsclient module without having to upgrade (if you can call it that) to windows 8?

  7. JV says:

    Tis works anywher on all current versions of WIndows:

    ipconfig /flushdns

    The DNS module is not avaliable before Win8/2012

  8. Not available in Windows 7 says:

    To make it clear, unless you are on Windows 8, or Server 2012, this is not available to you, regardless of what version of PowerShell you are running.

  9. Ed we would like a complete answer says:

    In a world with several flavors of MS OS’s operating in conjunction on networks, it would have been nice to have "BOTH" a W7 and a W8/2008/2012 Version of this script.

    We use this to "swap" to our DR site during our DR exercises. I use the quest activeroles – if you don’t have them use get-adcomputer for the $machines variable.

    $Creds = Get-Crediential
    $Machines = Get-QADComputer -DontUseDefaultIncludedProperties -IncludedProperties Name

    foreach ($Machine in $Machines)
    $FlushSession = New-PSSession -ComputerName $ -Authentication Default -Credential $Creds

    Invoke-Command -Session $Flushsession -ScriptBlock {ipconfig /flushdns}

    $RegSession = New-PSSession -ComputerName $ -Authentication Default -Credential $Creds

    Invoke-Command -Session $Regsession -ScriptBlock {ipconfig /flushdns}

Skip to main content