Scripting: Determine Workstation's IP Address
The following script determines the workstation's IP address.
ping %COMPUTERNAME% -4 -n 1 | find /i "ping statistics" > %TEMP%\results.txt
for /f "tokens=1-7,* delims=:. " %%a in ('type %TEMP%\results.txt') do set IP1=%%d& set IP2=%%e& set IP3=%%f& set IP4=%%g
echo Your IP is %IP1%.%IP2%.%IP3%.%IP4%
How It Works
Output from the PING command is piped into FIND to return only the results line, which is output to a text file. The contents of the text file is then fed into FOR which extracts the 4th (%%d), 5th (%%e), 6th (%%f) and 7th (%%g) parameters. These are then stored in environment variables so they are available to any further commands.
- Uses native Windows commands.
- Works on Windows 2000 and above (not tested Windows NT).
- Returns the most likely usable IP address the workstation is accessible on.
- Does not require local administrator or elevated privileges.
- Relies on current network allowing the machine to ping its own name.
- May return wifi address in preference to ethernet address.