Jump to content
MSFN is made available via donations, subscriptions and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. ×

Grab IP Using PowerShell

Recommended Posts

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
Link to comment
Share on other sites

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).

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...