Skip to main content

How to Get DNS Server Addresses in Batch Script

Knowing which DNS (Domain Name System) servers your computer is using is a fundamental step in network troubleshooting. When you can't connect to a website, one of the first things to check is if your computer can resolve the domain name to an IP address. Your configured DNS servers are responsible for this task. Scripts may need to retrieve this information to verify a network configuration, log diagnostic information, or check if a specific DNS server (like a corporate or public one) is in use.

This guide will teach you the two most effective methods for retrieving the list of DNS servers using built-in Windows commands. We will cover the classic ipconfig command, which is universally available, and the modern, more script-friendly PowerShell method.

The Core Method: ipconfig /all

The ipconfig utility is the standard tool for viewing network configuration. The /all switch provides a detailed report for all network adapters, which includes the list of DNS servers for each connection.

To find the relevant lines, we pipe the output of ipconfig /all to the findstr command.

ipconfig /all | findstr "DNS Servers"

This command will find and display the line(s) that list the configured DNS servers.

   DNS Servers . . . . . . . . . . . : 8.8.8.8
8.8.4.4

For modern systems, PowerShell provides a much cleaner and more direct way to get this information. The Get-DnsClientServerAddress cmdlet is designed for this purpose and returns structured data that is easy to work with.

Syntax: powershell -Command "(Get-DnsClientServerAddress).ServerAddresses"

This command queries the active network interfaces and returns a clean list of just the DNS server IP addresses, which is perfect for scripting.

Basic Example: Displaying the DNS Server List

This script runs both commands to show the difference in their output.

@ECHO OFF
ECHO --- Getting DNS Server Information ---
ECHO.

ECHO Method 1: Using ipconfig (human-readable)
ipconfig /all | findstr "DNS Servers"

ECHO.
ECHO Method 2: Using PowerShell (clean list for scripting)
powershell -Command "(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses"
note

The PowerShell command is filtered for IPv4 for a cleaner result.

How to Capture the DNS Servers in a Script

To use the DNS server list, you need to capture the output into variables. This is where the difference between the methods becomes very clear.

Script capturing ipconfig output

This is complex because the first DNS server is on the same line as the label, and subsequent servers are on new lines.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO --- Capturing DNS Servers (ipconfig method) ---
SET "count=0"

FOR /F "tokens=2 delims=:" %%A IN ('ipconfig /all ^| findstr "DNS Servers"') DO (
SET "dns_server=%%A"
SET "dns_server=!dns_server: =!"
SET /A "count+=1"
ECHO Server !count!: !dns_server!
)

ENDLOCAL

This method is far simpler, as PowerShell provides a clean list of just the IPs.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO --- Capturing DNS Servers (PowerShell method) ---
SET "count=0"

FOR /F %%S IN (
'powershell -Command "(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses"'
) DO (
SET /A "count+=1"
ECHO Server !count!: %%S
)

ENDLOCAL

How the Methods Work

  • ipconfig /all: This command dumps a large text report of all TCP/IP network configuration values. The findstr command then acts as a simple text filter on this report.
  • PowerShell Get-DnsClientServerAddress: This cmdlet directly queries the Windows network stack for the objects representing the configured DNS servers. It returns structured data, not just text, which is why we can easily ask for just the .ServerAddresses property for a clean output.

Common Pitfalls and How to Solve Them

Problem: The Output Shows Multiple Adapters

A computer can have multiple network adapters (Ethernet, Wi-Fi, VPN, virtual machine adapters). ipconfig /all will show the DNS servers for all of them, which can be confusing.

Solution: The PowerShell method is better for this. You can specify which interface to check.

powershell -Command "(Get-DnsClientServerAddress -InterfaceAlias 'Ethernet').ServerAddresses"

This gets the DNS servers for only the "Ethernet" adapter.

Problem: Parsing the ipconfig Output is Clumsy

As seen in the capture example, parsing ipconfig is tricky. You have to split by the colon (:), remove leading spaces, and handle multiple lines.

Solution: Use the PowerShell method. It is designed for scripting and provides a clean, predictable list of IP addresses without any extra text or formatting to deal with.

Practical Example: A "Check DNS" Diagnostic Script

This script uses the robust PowerShell method to get the list of DNS servers and then checks if a known, desired DNS server (like Google's 8.8.8.8) is configured.

@ECHO OFF
SETLOCAL
SET "DESIRED_DNS=8.8.8.8"
SET "IsConfigured=0"

ECHO --- DNS Configuration Check ---
ECHO Checking if '%DESIRED_DNS%' is a configured DNS server...
ECHO.

FOR /F %%S IN (
'powershell -Command "(Get-DnsClientServerAddress -AddressFamily IPv4).ServerAddresses"'
) DO (
ECHO Found configured DNS server: %%S
IF "%%S"=="%DESIRED_DNS%" SET "IsConfigured=1"
)

ECHO.
IF %IsConfigured% EQU 1 (
ECHO [SUCCESS] The desired DNS server is correctly configured.
) ELSE (
ECHO [FAILURE] The desired DNS server was not found.
)

ENDLOCAL

Conclusion

Retrieving the configured DNS servers is a key diagnostic step, and Windows provides several ways to do it.

  • The ipconfig /all | findstr method is the classic approach. It's universally available but produces output that is difficult to parse reliably in a script.
  • The PowerShell Get-DnsClientServerAddress cmdlet is the overwhelmingly recommended best practice. It is clean, powerful, easily parsable, and can target specific network adapters, making it the superior choice for any automation script.