How to Display the DNS Cache in Batch Script
The DNS (Domain Name System) cache is a temporary database maintained by Windows that stores the IP addresses of domain names you have recently visited. When you visit a website, your computer queries the cache first. If the record is there, it can connect much faster without having to ask an external DNS server. Viewing this cache is a critical step in network troubleshooting, allowing you to see what IP address your computer thinks a website has, which can help diagnose connectivity or "site not found" errors.
This guide will teach you how to use the built-in ipconfig command to display the contents of the DNS resolver cache. You will also learn how to save this information to a file and, more importantly, how to filter it to find specific entries.
The Core Command: ipconfig /displaydns
The ipconfig utility is the standard command-line tool for managing network interface configuration. It includes a specific switch to view the contents of the local DNS cache.
Syntax: ipconfig /displaydns
This single command instructs the DNS Client service to output its entire current cache to the command prompt.
Basic Example: Displaying the Full Cache
Running this command in a command prompt will immediately show you every DNS record your computer has recently cached.
@ECHO OFF
ECHO --- Displaying the local DNS Resolver Cache ---
ECHO.
ipconfig /displaydns
How to Interpret the Output
The output is a series of records, each with several fields.
www.google.com
----------------------------------------
Record Name . . . . . : www.google.com
Record Type . . . . . : 1
Time To Live . . . . : 210
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 142.250.189.228
localhost
----------------------------------------
Record Name . . . . . : localhost
Record Type . . . . . : 1
Data Length . . . . . : 4
Section . . . . . . . : Answer
A (Host) Record . . . : 127.0.0.1
- Record Name: The domain name that was looked up.
- Record Type:
1for anArecord (IPv4 address),28for anAAAArecord (IPv6 address). - Time To Live (TTL): How many more seconds this record will remain in the cache before it expires.
- A (Host) Record: The IP address that the domain name resolved to. This is often the most important piece of information.
Filtering the Cache for a Specific Domain (findstr)
The full DNS cache can be thousands of lines long. For troubleshooting, you usually only care about one specific domain. You can pipe the output of ipconfig /displaydns to the findstr command to filter the results.
This script will show all cached records related to "google.com".
@ECHO OFF
ECHO --- Searching for 'google.com' in the DNS Cache ---
ECHO.
ipconfig /displaydns | findstr /I "google.com"
/I: Makes the search case-Insensitive.
Output:
--- Searching for 'google.com' in the DNS Cache ---
www.google.com
Record Name . . . . . : www.google.com
play.google.com
Record Name . . . . . : play.google.com
A (Host) Record . . . : 142.250.74.13
Saving the Cache to a File for Analysis
If you need to analyze the full cache or send it to a support technician, it's best to save it to a file using output redirection (>).
@ECHO OFF
SET "OutputFile=%USERPROFILE%\Desktop\DNS_Cache_Log.txt"
ECHO --- Saving DNS Cache to a File ---
ECHO Output will be saved to: "%OutputFile%"
ipconfig /displaydns > "%OutputFile%"
ECHO.
ECHO [SUCCESS] DNS cache has been saved.
Common Pitfalls and How to Solve Them
Problem: The Output is Too Long
When run interactively, the output of ipconfig /displaydns can scroll by too quickly to read.
Solution: Pipe the output to the more command, which will pause the output after each screenful.
ipconfig /displaydns | more
Problem: Missing Information (Permissions)
While ipconfig /displaydns can be run by a standard user, on some locked-down systems or for certain types of records, running it from an elevated (Administrator) command prompt may provide a more complete and accurate view of the cache.
Solution: For any serious diagnostic work, it's a best practice to run your network troubleshooting scripts as an Administrator.
Practical Example: A "DNS Snapshot and Flush" Script
This is a very common troubleshooting sequence. The script first saves the current state of the DNS cache for analysis and then flushes it to force the computer to perform fresh DNS lookups for all future connections.
@ECHO OFF
SETLOCAL
TITLE DNS Troubleshooting Utility
SET "LogFolder=%~dp0Logs"
MKDIR "%LogFolder%" 2>NUL
SET "LogFile=%LogFolder%\DNS_Cache_Before_Flush.txt"
ECHO --- DNS Troubleshooting Script ---
ECHO.
ECHO This script will save the current DNS cache and then flush it.
PAUSE
ECHO Step 1: Saving a snapshot of the current DNS cache...
ipconfig /displaydns > "%LogFile%"
ECHO Snapshot saved to: "%LogFile%"
ECHO.
ECHO Step 2: Flushing the DNS Resolver Cache...
ipconfig /flushdns
ECHO.
ECHO --- Operation Complete ---
ENDLOCAL
Conclusion
The ipconfig /displaydns command is an essential tool for any network troubleshooting task. It gives you direct insight into how your computer is resolving domain names.
Key takeaways for using it in scripts:
- Use
ipconfig /displaydnsto view the entire cache. - Pipe the output to
findstrto filter for specific domains, which is the most common use case. - Redirect the output with
> filename.txtto save the full cache for later analysis. - For best results, run your diagnostic scripts as an Administrator.