How to Get the IP Address from a Hostname in Batch Script
In network scripting and troubleshooting, you often need to resolve a hostname (like www.google.com) to its corresponding IP address (like 142.250.191.174). This process, known as a DNS lookup, is essential for checking connectivity, logging server addresses, or verifying network configurations. While Windows has several built-in tools for this, some are much better suited for scripting than others.
This guide will teach you the classic methods using nslookup and ping, and then demonstrate the vastly superior and simpler modern approach using a PowerShell one-liner. For any reliable automation, the PowerShell method is the recommended best practice.
The Core Task: DNS Resolution
DNS resolution is the process of asking a DNS server, "What is the IP address for this hostname?" Every network tool, including your web browser, does this constantly. Our goal in scripting is to perform this lookup and capture the resulting IP address.
The Classic Method: Parsing nslookup
The nslookup (Name Server Lookup) command is the traditional, dedicated tool for querying DNS servers.
Command: nslookup google.com
Its output is verbose, designed for human administrators, and notoriously difficult to parse in a script.
Server: mydnsserver.com
Address: 192.168.1.1
Non-authoritative answer:
Name: google.com
Addresses: 2607:f8b0:4004:831::200e
142.250.191.174
The Simpler Method (Less Reliable): Parsing ping
The ping command's primary purpose is to check connectivity, but its first step is always to resolve the hostname to an IP address. We can extract the IP from its first line of output.
Command: ping -n 1 google.com
The IP address is right in the first line, making it easier to parse than nslookup.
Pinging google.com [142.250.191.174] with 32 bytes of data:
Reply from 142.250.191.174: bytes=32 time=12ms TTL=118
The Superior Method (Recommended): Using PowerShell
For modern systems, PowerShell provides a direct and clean way to perform a DNS lookup without any extra text or formatting.
Syntax: powershell -Command "[System.Net.Dns]::GetHostAddresses('google.com').IPAddressToString"
This command directly accesses the .NET DNS resolver, asks for the addresses, and formats them as strings. The output is a clean list of IP addresses, one per line, which is perfect for scripting.
Basic Example: Resolving "google.com"
This script runs the recommended PowerShell method to show its clean output.
@ECHO OFF
ECHO --- Resolving 'google.com' with PowerShell ---
powershell -Command "[System.Net.Dns]::GetHostAddresses('google.com').IPAddressToString"
Output:
--- Resolving 'google.com' with PowerShell ---
2607:f8b0:4004:831::200e
142.250.191.174
How to Capture the IP Address in a Variable
This is where the difference between the methods becomes critical.
Capturing with PowerShell (Easy and Recommended)
This is simple because the output is so clean. The FOR /F loop captures the first IP address found.
@ECHO OFF
SET "Hostname=google.com"
SET "IP_Address="
FOR /F %%I IN (
'powershell -Command "[System.Net.Dns]::GetHostAddresses('%Hostname%').IPAddressToString"'
) DO (
SET "IP_Address=%%I"
GOTO :Found
)
:Found
ECHO PowerShell Method: The IP for %Hostname% is %IP_Address%
Capturing with ping (Moderately Easy)
@ECHO OFF
SET "Hostname=google.com"
SET "IP_Address="
FOR /F "tokens=3 delims=[:]" %%I IN ('ping -n 1 %Hostname%') DO (
SET "IP_Address=%%I"
GOTO :Found
)
:Found
ECHO Ping Method: The IP for %Hostname% is %IP_Address%
This is fragile and can break if the output language of ping changes.
Capturing with nslookup (Difficult and Not Recommended)
Parsing nslookup is extremely brittle. You have to skip the first two lines and then find the line containing "Address" or "Addresses". This is not a reliable method for production scripts.
Common Pitfalls and How to Solve Them
Problem: The Hostname Has Multiple IP Addresses
Major websites often have multiple IP addresses for load balancing and redundancy.
pingwill only ever show you one IP address.nslookupand PowerShell will correctly list all of them (both IPv4 and IPv6).
Solution: The PowerShell method is superior. The FOR /F loop shown will capture the first IP in the list. If you need to see all of them, you can simply remove the GOTO :Found to let the loop run to completion.
Problem: The Hostname Cannot Be Found
If the DNS lookup fails, each tool gives a different error.
ping:Ping request could not find host...nslookup:*** server can't find google.invalid: Non-existent domain.- PowerShell: Throws an error.
Solution: For scripting, you should check the %ERRORLEVEL% of the command.
powershell -Command "[System.Net.Dns]::GetHostAddresses('fakehostname')" > NUL 2> NUL
IF %ERRORLEVEL% NEQ 0 (
ECHO Hostname could not be resolved.
)
Problem: Parsing nslookup is Very Difficult
As mentioned, the output format of nslookup is verbose and not designed for machines.
Solution: Do not use nslookup for parsing in a script. Use the PowerShell method, as it is designed for automation and provides clean, predictable output.
Practical Example: A Simple "Resolve Host" Utility
This script takes a hostname as a command-line argument and returns the first IP address it finds, using the robust PowerShell method.
@ECHO OFF
SETLOCAL
SET "Hostname=%~1"
IF "%Hostname%"=="" (
ECHO [ERROR] Please provide a hostname.
ECHO Usage: %~n0 <hostname>
GOTO :End
)
ECHO --- Resolving Hostname: %Hostname% ---
SET "IP_Address="
FOR /F %%I IN (
'powershell -NoProfile -ExecutionPolicy Bypass -Command "[System.Net.Dns]::GetHostAddresses('%Hostname%').IPAddressToString"'
) DO (
SET "IP_Address=%%I"
GOTO :Found
)
:Found
IF NOT DEFINED IP_Address (
ECHO [FAILURE] Could not resolve the hostname.
) ELSE (
ECHO [SUCCESS] IP Address: %IP_Address%
)
:End
ENDLOCAL
Conclusion
While Windows offers several tools for DNS lookups, they are not all created equal when it comes to scripting.
- The
pingcommand is a quick and dirty method, but it is unreliable and only returns one IP address. - The
nslookupcommand is a powerful interactive tool, but its verbose output is not suitable for parsing in a script. - The PowerShell
[System.Net.Dns]::GetHostAddresses()method is the overwhelmingly recommended best practice. It is clean, fast, reliable, handles multiple IPs correctly, and is the professional choice for any automation script.