Get IPv4 and IPv6 using command line

This little batch file prints out either the machine’s IPv4 or IPv6 (based on the given argument – ‘4’ or ‘6’). It works on Windows 7 and 2008 (and probably later editions as well).

Update 25-Nov-2013: The IPv6 format was not correct in some cases. I’ve updated the script below with the fix

@echo off
 if "%1"=="" goto usage
 if %1==4 (
 set ip_address_string="IPv4 Address"
 for /f "usebackq tokens=2 delims=':'" %%f in (`ipconfig ^| findstr /c:"IPv4 Address"`) do echo %%f
 goto :eof
 ) else if %1==6 (
 for /f "usebackq tokens=5" %%i in (`netsh interface ipv6 show address ^| findstr /c:"Manual"`) do echo %%i
 goto :eof
 )
:usage
 echo Usage: %0 ip_type
 echo Where ip_type is either 4 or 6
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s