Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Grab IP Using PowerShell

- - - - -

  • Please log in to reply
4 replies to this topic



    Advanced Member

  • Member
  • PipPipPip
  • 412 posts
  • Joined 28-April 04
I'm on a computer running Windows XP with PowerShell installed and I would like to be able to grab the IP address from the output of ipconfig, then place it into a variable that will be used as an argument when executing a program. I use Linux on my home computers, so I would know how to do it with shell scripting, but PowerShell is completely different (it reminds me of Java).

I've gotten as far as storing the output of ipconfig in a variable but I don't know how to "grep" it out of all the ouput since I'm not good with RegEx and then store that string into a variable which will be called upon later.

C:\Documents and Settings\bran>ipconfig

Windows IP Configuration

Ethernet adapter Local Area Connection:

        Connection-specific DNS Suffix  . : domain.net
        IP Address. . . . . . . . . . . . :
        Subnet Mask . . . . . . . . . . . :
        Default Gateway . . . . . . . . . :

Ethernet adapter Local Area Connection 2:

        Connection-specific DNS Suffix  . :
        IP Address. . . . . . . . . . . . :
        Subnet Mask . . . . . . . . . . . :
        Default Gateway . . . . . . . . . :

The IP address that I would like to be able to grab is that of Ethernet adapter Local Area Connection 2, the first three octets are always the same (192.168.42) it is only the last octet that changes.

So in short I would like something along the lines of this:
echo $ip [grab IP address and store it in a variable]
forcebindip $ipaddress c:\program.exe

or for those of you that understand shell scripting
ipconfig | grep "192.168.42"| head -n1| cut -c 30 > $ip
forcebindip $ip c:\program.exe

Edited by Brando569, 13 December 2012 - 10:57 AM.

How to remove advertisement from MSFN



    The Finder

  • Developer
  • 16,300 posts
  • Joined 23-July 04
  • OS:none specified
  • Country: Country Flag
NOT what you asked for, but in batch:
the code is "botched" you may want to use this:

Basically it amounts to:

for /f "tokens=2 delims=:" %%A in ('IPCONFIG^|FIND "IP"') do echo %%A




    Not really Newbie

  • Member
  • PipPipPipPipPipPipPip
  • 1,804 posts
  • Joined 13-January 06
Taken from there:
((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
It only retrieve the first ip address if you have more than one network adapter.



    Wise Owl

  • Super Moderator
  • 4,078 posts
  • Joined 13-October 04
  • OS:Windows 7 x64
  • Country: Country Flag


Just a quick follow up batch file using ideas from both jaclaz and allen2:

REM Change the line below to reflect your known IP string

SET KIP=192.168.42


	CALL :SUB %%#)




The SUB currently only shows you the relevant IP Address, you would obviously replace that with the code you wish to run, (and when you're happy REMove the PAUSE).



    Gustatus similis pullus

  • Patrons
  • 11,031 posts
  • Joined 09-September 01
  • OS:Windows 10 x64
  • Country: Country Flag


I don't know if this works on XP or not as I don't have a box at the moment to check, but if XP has the "Get-NetIPAddress" cmdlet, you could start with this and go from there:
Get-NetIPAddress | Sort-Object -Property InterfaceIndex | Format-Table

MCTS Windows Internals, MCITP Server 2008 EA, MCTS MDT/BDD, MCSE/MCSA Server 2003, Server 2012, Windows 8
Please read the rules before posting!
Please consider donating to MSFN to keep it up and running!

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users