PowerShell Pipeline
Using PowerShell To Work with the DNS Client
Replace your legacy ipconfig switches with these cmdlets in the DNSClient module.
With the addition of the DNSClient module in PowerShell, you can take some of the old ipconfig commands that we know and love and make them more robust using PowerShell.
Also available are a number of other cmdlets within the module that you might be able to find a use for in your day-to-day operations.
First, let's take a look at all of the cmdlets which are available within the DNSClient module.
PS C:\> Get-Command -Module DNSClient
CommandType Name Version Source
----------- ---- ------- ------
Function Add-DnsClientNrptRule 1.0.0.0 DNSClient
Function Clear-DnsClientCache 1.0.0.0 DNSClient
Function Get-DnsClient 1.0.0.0 DNSClient
Function Get-DnsClientCache 1.0.0.0 DNSClient
Function Get-DnsClientGlobalSetting 1.0.0.0 DNSClient
Function Get-DnsClientNrptGlobal 1.0.0.0 DNSClient
Function Get-DnsClientNrptPolicy 1.0.0.0 DNSClient
Function Get-DnsClientNrptRule 1.0.0.0 DNSClient
Function Get-DnsClientServerAddress 1.0.0.0 DNSClient
Function Register-DnsClient 1.0.0.0 DNSClient
Function Remove-DnsClientNrptRule 1.0.0.0 DNSClient
Function Set-DnsClient 1.0.0.0 DNSClient
Function Set-DnsClientGlobalSetting 1.0.0.0 DNSClient
Function Set-DnsClientNrptGlobal 1.0.0.0 DNSClient
Function Set-DnsClientNrptRule 1.0.0.0 DNSClient
Function Set-DnsClientServerAddress 1.0.0.0 DNSClient
Cmdlet Resolve-DnsName 1.0.0.0 DNSClient
In this module, we have 17 cmdlets available to us. For this article, I will leave out the cmdlets dealing with Nrtp and focus more on the cmdlets that provide familiar information that we have seen in the past using ipconfig and one of its many switches.
If you wanted to view the DNS cache that you have on your system, you would have to usually use the /DisplayDNS switch on ipconfig.
PS C:\> ipconfig /displaydns
Windows IP Configuration
65.186.168.192.in-addr.arpa
----------------------------------------
Record Name . . . . . : 65.186.168.192.in-addr.arpa.
Record Type . . . . . : 12
Time To Live . . . . : 86400
Data Length . . . . . : 8
Section . . . . . . . : Answer
PTR Record . . . . . : PROX-PC.mshome.net
prox-pc.mshome.net
----------------------------------------
No records of type AAAA
prox-pc.mshome.net
----------------------------------------
Record Name . . . . . : PROX-PC.mshome.net
Record Type . . . . . : 1
Time To Live . . . . : 86400
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 192.168.186.65
While it does provide the information that we might be interested in, it is being presented to us as text on the console, not something that we can easily work with unless you want to bring out the RegEx wizards to start making this data more manageable. Instead, we can use the Get-DNSClientCache cmdlet and see a much friendlier display of objects that we can then filter and sort as needed.
PS C:\> Get-DnsClientCache
Entry RecordName Record Status Section TimeTo Data Data
Type Live Length
----- ---------- ------ ------ ------- ------ ------ ----
65.186.168.192.in-addr... 65.186.168.192.in-addr... PTR Success Answer 86400 8 PROX-PC.mshome.net
prox-pc.mshome.net PROX-PC.mshome.net A Success Answer 86400 4 192.168.186.65
prox-pc.mshome.net AAAA NoRecords
What about clearing out the cache? Using ipconfig, we would make use of the /flushdns switch to perform this operation.
PS C:\> ipconfig /flushdns
Windows IP Configuration
Successfully flushed the DNS Resolver Cache.
With the Clear-DNSClientCache cmdlet, we simply run the command (there are no parameters necessary here unless you use -Verbose) and we will have accomplished the same action as we did running the ipconfig command.
PS C:\> Clear-DnsClientCache -Verbose
VERBOSE: The specified name resolution records cached on this machine will be removed.
Subsequent name resolutions may return up-to-date information.
Assuming that you allow your systems to register their own records on a DNS server, then you might know of using ipconfig /registerdns to force the client to register its records in DNS. Remember that you will need to run your console in an elevated prompt in order for this to work properly.
PS C:\> ipconfig /registerdns
Windows IP Configuration
Registration of the DNS resource records for all adapters of this computer has been initiated. Any errors will b
e reported in the Event Viewer in 15 minutes.
The same approach in PowerShell is to use Register-DNSClient, again without any parameters, to register the system to the DNS server. Similar to my example with clearing the cache, I am using Verbose to show a message of what is happening when running the cmdlet.
PS C:\> Register-DnsClient -Verbose
VERBOSE: This machine's DNS records will be updated on the DNS server.
This may affect the results of DNS queries for this machine.
A couple of other cmdlets that are not easily viewable when using ipconfig /all are the Get-DNSClient and Get-DnsClientServerAddress cmdlets. In fact, you won't be able to find anything when using ipconfig when you use Get-DNSClient.
PS C:\> Get-DnsClient
InterfaceAlias Interface ConnectionSpecificSuffix ConnectionSpecificSuffix RegisterThisConn UseSuffixWhen
Index SearchList ectionsAddress Registering
-------------- --------- ------------------------ ------------------------ ---------------- -------------
vEthernet (Internal) 8 {} True True
vEthernet (Default Switch) 22 {} False False
vEthernet (External) 18 {} True False
Local Area Connection* 2 13 {} True True
Loopback Pseudo-Interface 1 1 {} True True
Lastly, using Get-DNSClientServerAddress is a great way to quickly see what each of your adapters have configured for DNS.
PS C:\> Get-DnsClientServerAddress
InterfaceAlias Interface Address ServerAddresses
Index Family
-------------- --------- ------- ---------------
vEthernet (Internal) 8 IPv4 {}
vEthernet (Internal) 8 IPv6 {}
vEthernet (Default Switch) 22 IPv4 {}
vEthernet (Default Switch) 22 IPv6 {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
vEthernet (External) 18 IPv4 {192.168.1.1}
vEthernet (External) 18 IPv6 {}
Local Area Connection* 2 13 IPv4 {}
Local Area Connection* 2 13 IPv6 {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
Loopback Pseudo-Interface 1 1 IPv4 {}
Loopback Pseudo-Interface 1 1 IPv6 {fec0:0:0:ffff::1, fec0:0:0:ffff::2, fec0:0:0:ffff::3}
So with that, we have taken a quick look at some of the cmdlets which are in the DNSClient module. Some of these will instantly be great tools in your toolbox and will replace the legacy ipconfig switches with cmdlets that display objects to help with filtering data versus working with text output.
About the Author
Boe Prox is a Microsoft MVP in Windows PowerShell and a Senior Windows System Administrator. He has worked in the IT field since 2003, and he supports a variety of different platforms. He is a contributing author in PowerShell Deep Dives with chapters about WSUS and TCP communication. He is a moderator on the Hey, Scripting Guy! forum, and he has been a judge for the Scripting Games. He has presented talks on the topics of WSUS and PowerShell as well as runspaces to PowerShell user groups. He is an Honorary Scripting Guy, and he has submitted a number of posts as a to Microsoft's Hey, Scripting Guy! He also has a number of open source projects available on Codeplex and GitHub. His personal blog is at http://learn-powershell.net.