How do I copy the IPv4 IP Address from ipconfig to clipboard?

I want to get the ipv4 192.168.1.* from the ipconfig. I'm learning scripting and want to make an output value message box that displays the ip and copies it to clipboard. I have already done this by using the registry, but that is computer-specific as the adapters are unique per computer. So how do I get the computer to grab the 192.168.1.* for any computer using ipconfig, and copy it to the clipboard?


Solution 1:

ipconfig /all|clip

So the first part is the ipconfig /all command. You already know that one. Next is a vertical line |, called a pipe. It "pipes" the output of the command to the left of it to another place. clip simply means the clipboard.

Note that not every local IP starts with 192.168..; cool people like me use 10.0.0.*.

If you want just the IP address, and not the rest of the output of the command, please look at the accepted answer over at this question: How do I extract the IPv4 IP Address from the output of ipconfig

I edited that answer a bit so that it does what you want:

@echo off
setlocal
setlocal enabledelayedexpansion
rem throw away everything except the IPv4 address line 
for /f "usebackq tokens=*" %%a in (`ipconfig ^| findstr /i "ipv4"`) do (
  rem we have for example "IPv4 Address. . . . . . . . . . . : 192.168.42.78"
  rem split on : and get 2nd token
  for /f delims^=^:^ tokens^=2 %%b in ('echo %%a') do (
    rem we have " 192.168.42.78"
    rem split on . and get 4 tokens (octets)
    for /f "tokens=1-4 delims=." %%c in ("%%b") do (
      set _o1=%%c
      set _o2=%%d
      set _o3=%%e
      set _o4=%%f
      rem strip leading space from first octet
      set _4octet=!_o1:~1!.!_o2!.!_o3!.!_o4!
      echo !_4octet!|clip
      )
    )
  )
rem add additional commands here
endlocal

Credits: DavidPostill

Copy that to a textfile. Rename the file whatever.bat. Doubleclick on it to run it. It is a Batch file.