What It Does
When the Get-DnsClientCache cmdlet is available, the script counts the entries in the Windows DNS client cache. It clears it with Clear-DnsClientCache (or ipconfig /flushdns on a machine without the cmdlet), and counts again when the count cmdlet is available. Hosts-file entries stay in the count, and Windows starts refilling the cache as programs look up names.
With RegisterDns set to true it also runs ipconfig /registerdns, which asks the PC to register its name and addresses with its DNS server again. That is useful when other machines cannot find this one by name.
It does not edit the hosts file, change which DNS servers the adapter uses, or clear any other cache. If the count is zero and RegisterDns is off, it leaves the cache alone and says so.
Before You Run It
- If many PCs fail to resolve the same name, the problem is on the DNS server or upstream, not in each PC's cache.
- Check the hosts file by hand if a single name keeps resolving wrongly after a flush. This script leaves it alone.
- Use RegisterDns when other machines cannot reach this one by name, not as a habit on every run.
Type and Risk
- Type: Action. It makes its change, checks the change worked and says what changed. It exits 0, or 2 when it could not do the job.
- Risk: Low. Set DryRun=true, or run it with -WhatIf, to list every change without making any.
- What it changes: Clears the DNS client cache; optionally re-registers DNS records.
- Restart: It never restarts the machine.
- Typical run: Under 10 seconds.
- Example answer: "Flushed the DNS cache (87 entries before, 4 after)"
- Tested before listing on Windows 11 with Windows PowerShell 5.1, run as SYSTEM, against its own test cases.
Inputs
Pass an input as an argument (-Name value), as an environment variable with the same name, which is how most RMMs pass script variables, or change its default in the param() block at the top of the script. An argument wins over an environment variable, which wins over the default.
- RegisterDns: true to also run ipconfig /registerdns, which re-registers this computer's name and addresses with its DNS server. Default false.
Every script also takes DryRun, OutputFormat (Text, Answer, Json or Csv), ExportPath and ResultField, which writes the answer to an RMM custom field.
License
Provided "as is" under the MIT License, without warranty of any kind. Test before production use. MSP Outpost and User First Digital Inc. accept no liability for any damage or loss.