Skip to content

PS-Tip #1 – Remote DNS Cache Clearing

PS-Tips  are a series of shorter posts which introduce and explain short PowerShell snippets that may help to automate some tasks.

There are a requirements to run remote PowerShell commands and I’ll cover the details of setting the prerequisites in a later post. The process for getting setup isn’t too difficult so if it doesn’t work right away, make sure the following are setup on your systems:

  • Windows PowerShell 2.0 or later
  • .NET Framework 3.0 or later
  • Windows Remote Management 2.0 or later

Today’s PowerTip deals with remotely executing the ipconfig /flushdns  command on other systems.

The code is as follows:

$Computer = hostname #Replace hostname with the name of the system you want to execute the command on

Invoke-Command -script {Clear-DnsClientCache } -ComputerName $Computer

 

That’s it, pretty simple. The logic isn’t too complicated and structre is like this:

  1. Create a variable $Computer with the name of the target (remote) system
  2. Use the Invoke-Command cmdlet to run a script (-Script) on a target computer (-ComputerName)
  3. The {Clear-DnsClientCache} command is actually just the ‘Clear-DnsClientCache’ command defined as block command (the space between the {} is the block command)

So the execution is as follows:

  1. A variable is defined
  2. Invoke command creates a script which executes the ‘Clear-DNSClientCache’ command and then runs it on the target $Computer.

That’s all there is to it. I wrote this script after making some DNS changes and having users call in because the change hadn’t propagated yet. Instead of walking through the process of getting them in to a terminal and typing ‘ipconfig /flushdns’ I could just target their computer name and execute this. You can even create an array of computers and do a ForEach loop on all of them and get everything changed at once!

Let me know if you like the script or want to expand it! Send me any questions or comments or topics you’d like to see covered and we’ll explore more as time goes on. You can see more examples of cool PowerShell tricks at my GitHub here: https://github.com/theabraxas/IT-Infrastructure-Super-Utility

Thanks for reading!

Leave a Reply

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