SkillPilotAI

PowerShell DnsClient module

The DnsClient module is the PowerShell side of Windows name resolution: Resolve-DnsName (PowerShell's nslookup/dig), the client cache, DNS server addresses and suffixes, DNS over HTTPS (DoH) and the NRPT that sends some namespaces to specific DNS servers. It is the main toolkit for DNS troubleshooting on Windows.

216.1 Why is it used?

  • Query any record type from any server with object output (Resolve-DnsName).
  • Inspect and flush the local DNS cache, and see/set the DNS servers of each interface.
  • Encrypt DNS with DoH templates.
  • On VPN/DirectAccess, route corp namespaces to the company DNS server with NRPT rules.

216.2 Where it runs

  • Built into Windows 10/11 and Server; Windows PowerShell 5.1 and PowerShell 7. The module has 21 cmdlets.
  • Get- cmdlets only read; Add-, Set-, Remove-, Clear-, Register- need an Administrator PowerShell.
  • Linux/macOS have no such module: dig and resolvectl do the job.
  • Common parameters: -AsJob, -CimSession, -ThrottleLimit, -WhatIf, -Confirm.
PowerShell
Get-Command -Module DnsClient | Measure-Object
Output
PS> Get-Command -Module DnsClient | Measure-ObjectCount    : 21Average  :Sum      :Maximum  :Minimum  :Property :

216.3 How Windows resolves a name

Name resolution: cache, then DNS server
  1. 1.Browser asks for example.com

  2. 2.DNS Client service (Dnscache) checks its cache; hosts file entries are loaded into it

  3. 3.Cache miss: NRPT rule decides if a special DNS server is used

  4. 4.Otherwise query goes on UDP 53 via the router to the interface's DNS server

  5. 5.Answer comes back with a TTL (e.g. 300 s) and is cached

  6. 6.Next request = cache hit, immediate answer

216.4 Resolve-DnsName

PowerShell
Resolve-DnsName localhost -CacheOnlyResolve-DnsName localhost -DnsOnly
Output
PS> Resolve-DnsName localhost -CacheOnlyName       : localhostQueryType  : AAAATTL        : 1200Section    : QuestionIP6Address : ::1Name       : localhostQueryType  : ATTL        : 1200Section    : QuestionIP4Address : 127.0.0.1PS> Resolve-DnsName localhost -DnsOnlyName       : localhostQueryType  : ATTL        : 600Section    : AnswerIP4Address : 127.0.0.1
OptionMeaning
-Name www.bing.comName to resolve (required)
-Type A|AAAA|MX|TXT|SRV|NS|CNAME|SOA|PTRRecord type; default A_AAAA = both A and AAAA
-Server 10.0.0.1Ask this DNS server directly
-DnsOnlyOnly DNS, no LLMNR/NetBIOS
-CacheOnlyOnly the local cache
-NoHostsFileSkip the hosts file
-TcpOnly / -NoRecursion / -QuickTimeoutTCP only / no recursion / short timeouts
-DnssecOk / -DnssecCdSet DNSSEC OK / checking-disabled bits
-LlmnrOnly / -LlmnrFallback / -NetbiosFallbackLocal name protocols

Linux equivalent (real Docker lab with a dnsmasq server at 192.168.236.53 for lab.local):

Terminal
cat /etc/resolv.confdig @192.168.236.53 web.lab.local A +noall +answerdig @192.168.236.53 web.lab.local AAAA +noall +answerdig @192.168.236.53 lab.local MX +noall +answerdig @192.168.236.53 lab.local TXT +noall +answerdig @192.168.236.53 _ldap._tcp.lab.local SRV +noall +answer
Output
$ cat /etc/resolv.confnameserver 127.0.0.11options ndots:0# ExtServers: [192.168.236.53]$ dig @192.168.236.53 web.lab.local A +noall +answerweb.lab.local.    300  IN  A  192.168.236.80$ dig @192.168.236.53 web.lab.local AAAA +noall +answerweb.lab.local.    300  IN  AAAA  fd00:236::80$ dig @192.168.236.53 lab.local MX +noall +answerlab.local.    300  IN  MX  10 mail.lab.local.$ dig @192.168.236.53 lab.local TXT +noall +answerlab.local.    300  IN  TXT  "v=spf1 mx -all"$ dig @192.168.236.53 _ldap._tcp.lab.local SRV +noall +answer_ldap._tcp.lab.local.  300  IN  SRV  0 100 389 dc1.lab.local.
Terminal
dig @192.168.236.53 web.lab.local +tcpdig @192.168.236.53 nope.lab.localdig @192.168.236.53 example.comgetent hosts demo.lab.localdig @192.168.236.53 demo.lab.local
Output
$ dig @192.168.236.53 web.lab.local +tcp;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 6370web.lab.local.    300  IN  A  192.168.236.80;; SERVER: 192.168.236.53#53(192.168.236.53) (TCP)$ dig @192.168.236.53 nope.lab.local;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 23914$ dig @192.168.236.53 example.com;; ->>HEADER<<- opcode: QUERY, status: REFUSED, id: 20528$ getent hosts demo.lab.local192.168.236.99    demo.lab.local  demo.lab.local$ dig @192.168.236.53 demo.lab.local;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 51747
  • +tcp = -TcpOnly; NXDOMAIN = name does not exist; REFUSED because the lab server has recursion and upstream off.
  • demo.lab.local is only in /etc/hosts: getent resolves it, dig gets NXDOMAIN (like -NoHostsFile).

216.5 Cache, client settings, servers and suffixes

CmdletDoes
Get-DnsClientCacheCache entries; filter by -Entry, -Type, -Status, -TimeToLive
Clear-DnsClientCacheDeletes the whole local cache (= ipconfig /flushdns)
Register-DnsClientDNS dynamic update (= ipconfig /registerdns)
Get-/Set-DnsClientPer-interface suffix and registration policy
Get-/Set-DnsClientServerAddressDNS servers per interface; -ResetServerAddresses (back to DHCP), -Validate
Get-/Set-DnsClientGlobalSettingSuffix search list, UseDevolution, DevolutionLevel
PowerShell
# Example (docs), admin only, NOT run here:Set-DnsClient -InterfaceIndex 12 `  -ConnectionSpecificSuffix "corp.contoso.com"Set-DnsClientServerAddress -InterfaceIndex 12 `  -ServerAddresses ("10.0.0.1","10.0.0.2")Set-DnsClientServerAddress -InterfaceIndex 12 `  -ResetServerAddressesGet-DnsClientGlobalSetting
Output
PS C:\> Get-DnsClientGlobalSettingUseSuffixSearchList : TrueSuffixSearchList    : {corp.contoso.com, na.corp.contoso.com}UseDevolution       : TrueDevolutionLevel     : 0
  • The suffix search list is tried for short names; with UseDevolution True the resolver drops the left label and tries the parent suffix too.

216.6 DNS over HTTPS (DoH)

PowerShell
Add-DnsClientDohServerAddress -ServerAddress 10.23.1.1 `  -DohTemplate https://adatum.com/dns-query `  -AllowFallbackToUdp $True -AutoUpgrade $TrueGet-DnsClientDohServerAddress -ServerAddress 10.23.1.1,10.18.1.1Set-DnsClientDohServerAddress -ServerAddress 10.23.1.1 `  -DohTemplate https://adatum.com/dns-query -AutoUpgrade $TrueRemove-DnsClientDohServerAddress -ServerAddress 10.23.1.1,10.18.1.1
  • Add- pairs a DNS server IP with its HTTPS template; -AutoUpgrade sends every query to that server over DoH (port 443).
  • -AllowFallbackToUdp allows plain DNS when DoH fails.
  • Set-: a switch you do not give becomes False (docs).

216.7 NRPT (Name Resolution Policy Table)

NRPT rule and DoH: where the query goes
  1. 1.Office laptop on VPN resolves app.corp.local

  2. 2.NRPT rule: namespace corp.local -> DNS server 10.0.0.1

  3. 3.Query goes only to the corp DNS

  4. 4.Other names go to the interface's normal DNS server

  5. 5.If that server has a DoH template the query is encrypted over HTTPS 443

PowerShell
# Example (docs), admin only, NOT run here:Add-DnsClientNrptRule -Namespace "pqr.com" `  -NameEncoding "Punycode" `  -NameServers "10.1.1.1" -PassThru
Output
PS C:\> Add-DnsClientNrptRule -Namespace "pqr.com" -NameEncoding "Punycode" -NameServers "10.1.1.1" -PassThruName                             : {6a78d8d1-231d-4d1e-bc23-fb593e11a53d}Namespace                        : {pqr.com}DirectAccessEnabled              : FalseNameServers                      : 10.1.1.1DnsSecEnabled                    : FalseNameEncoding                     : Punycode
CmdletKey options
Add-DnsClientNrptRule-Namespace (required), -NameServers, -NameEncoding, -DnsSecEnable, -DnsSecIPsecRequired, -GpoName, -DAEnable, -DANameServers, -DAProxyType
Set-DnsClientNrptRule-Name (rule GUID, required); same options but switches need $True/$False
Get-/Remove-DnsClientNrptRule-Name, -GpoName, -Server; Remove -Force skips confirmation
Get-/Set-DnsClientNrptGlobal-EnableDAForAllNetworks, -QueryPolicy, -SecureNameQueryFallback
Get-DnsClientNrptPolicy-Effective: the policy really applied; -Namespace for one name
PowerShell
Get-DnsClientNrptPolicy -Effective -Namespace "nls.corp.contoso.com"
Output
Namespace                        : nls.corp.contoso.comQueryPolicy                      : QueryIPv6OnlySecureNameQueryFallback          : FallbackPrivateDirectAccessEnabled              : TrueDirectAccessProxyType            : UseDefaultNameEncoding                     :

216.8 Common mistakes

  • Thinking Clear-DnsClientCache gives you a website's new IP: it only clears the PC cache; the DNS server keeps its old record until the TTL runs out.
  • Testing without -DnsOnly / -NoHostsFile and getting an answer from the hosts file or LLMNR.
  • Debugging VPN name failures without checking Get-DnsClientNrptPolicy -Effective.
  • Using Set-DnsClientDohServerAddress without all switches: missing ones become False.

216.9 Interview tips

  • A record from one server, no hosts/cache: Resolve-DnsName name -Server <ip> -Type A -DnsOnly -NoHostsFile.
  • DNS troubleshooting order: Resolve-DnsName (compare with -Server), Get-DnsClientServerAddress, Get-/Clear-DnsClientCache, Get-DnsClientNrptPolicy -Effective on VPN.
  • Clear-DnsClientCache = ipconfig /flushdns; Register-DnsClient = ipconfig /registerdns.
  • NRPT = choose the DNS server by namespace; DoH = DNS encrypted over HTTPS 443.