6

I would like to use Get-WmiObject Win32_NetworkAdapterConfiguration to return the ip address of a network card. Unfortunately, I cannot figure out how to format the output to display only the IPv.4 address.

Get-WmiObject Win32_NetworkAdapterConfiguration | Select IPAddress | Where-Object {$_.IPaddress -like "192.168*"}

Displays:

IPAddress
---------
{192.168.56.1, fe80::8980:15f4:e2f4:aeca}

Using the above output as an example, I would like it to only return 192.168.56.1 (Some clients have multiple NIC's, hence the "Where-Object")

6 Answers 6

10

The IPAddress property is a string[], so the following should do it:

gwmi Win32_NetworkAdapterConfiguration |
    Where { $_.IPAddress } | # filter the objects where an address actually exists
    Select -Expand IPAddress | # retrieve only the property *value*
    Where { $_ -like '192.168.*' }
Sign up to request clarification or add additional context in comments.

2 Comments

The above would only work for IPv4 addresses beginning "192.168.*". If you replace the last Where clause with "{$_ -notlike ':'}" it will give you just the IPv4 address.
I needed to use ?{$_ -notlike "*:*"}.
4

Adding a quicker answer (avoiding Where-Object and using -like operation on a list):

@(@(Get-WmiObject Win32_NetworkAdapterConfiguration | Select-Object -ExpandProperty IPAddress) -like "*.*")[0]

Hope this Helps

Comments

1

Thought I would share my own variation on the above, in case it helps someone. Just one line:

Get-WmiObject win32_networkadapterconfiguration | where { $_.ipaddress -like "1*" } | select -ExpandProperty ipaddress | select -First 1

Cheers.

Comments

1
(Get-WmiObject Win32_NetworkAdapterConfiguration | where { (($_.IPEnabled -ne $null) -and ($_.DefaultIPGateway -ne $null)) } | select IPAddress -First 1).IPAddress[0]

Returns the IP-address of network connection with default gateway. This is exactly what you need in most cases :)

Compatible with Powershell 2.0 (Windows XP) and newer.

Comments

0

(Get-WmiObject win32_Networkadapterconfiguration | Where-Object{$_.ipaddress -notlike $null}).IPaddress | Select-Object -First 1

Hope that this will help !

1 Comment

Could you please elaborate more your answer adding a little more description about the solution you provide?
0
(Get-WMIObject -Class Win32_NetworkAdapterConfiguration).IPAddress

2 Comments

Why do you prefer this formulation over existing answers that have been validated by the community?
I just préfet this formulation to ne very simple to return thé IP address without any other parameters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.