Windows 10

Batch File To Get IP Address

In this tutorial, we will demonstrate how to extract the IPv4 address of your Windows machine using a batch script. This is especially useful for network diagnostics, logging, or automating network-related tasks.

Batch files automate sequences of DOS (Disk Operating System) commands, enabling quick execution of system utilities like ipconfig.
 

🧪 Example: Batch File to Retrieve the IPv4 Address

The ipconfig command displays network configuration details, and by piping its output to find, we can filter the lines containing the IPv4 address.
 
✅ Script:

@echo off
ipconfig | find "IPv4" /i

🧾 Sample Output:

IPv4 Address. . . . . . . . . . . : 192.168.0.145

🔍 Explanation:

  • ipconfig lists all network adapters and their configuration.
  • | pipes the output of ipconfig to the next command.
  • find “IPv4” filters lines containing the string “IPv4” (case-insensitive due to /i).
  • This method shows only the IPv4 address line(s), making the output concise and easy to parse.

⚙️ Advanced Tip:
To capture the IP address in a variable for further processing within the script, you can use a for /f loop:

@echo off
for /f "tokens:2 delims::" %%a in ('ipconfig ^| find "IPv4"') do (
  set ip=%%a
)
echo Your IP address is:%ip%

This trims and stores the IP address for use in later commands.
 

✅ Summary

Using ipconfig combined with find in a batch script provides a simple and effective way to retrieve your machine’s IPv4 address for automation or monitoring purposes.
 

One thought on “Batch File To Get IP Address

  • This is not working as a .bat file. If I run this in CMD directly it works but if I save the command into a .bat file all I get is a blank command window.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *