Reputation: 55
I have several network adapters on my PC.
I want to simply get the IP Address v4 (with no headers or extras) of the adapter called a specific name and store it into a variable for further use.
I am able to return the IP Address with headers but not what I want.
Please Help - thanks.
Here is what I tried:
$ipa = Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } |
Select-Object { $_.IPAddress }
Edit:
$ipa = Get-NetIPAddress | where {$_.InterfaceAlias -eq "LAN2" -and
$_.AddressFamily -eq "IPv4"} | select -expandproperty ipaddress
The above code is returning both my wired adaptors' addresses but it is returning just the Ip address at least and nothing else (thanks Anthony)
There is ONLY one called "LAN2" - i only need that one - so still stuck
Update : Austin Frenchs second solution works great - will test Owls today later - thanks everyone so far - great helpful community
Upvotes: 1
Views: 405
Reputation: 1026
simple one liner
(Get-NetIPConfiguration).ipv4address | where {$_.InterfaceAlias -eq "Lan2"} | foreach { write-host $_.ipaddress}
get from enabled network
Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE | select Ipaddress
only from particular adapter and ipv4
Get-WMIObject win32_NetworkAdapterConfiguration |
Where-Object { $_.IPEnabled -eq $true } |
Foreach-Object { $_.IPAddress } |
Foreach-Object { [IPAddress]$_ } |
Where-Object { $_.AddressFamily -eq 'LAN2' } |
Foreach-Object { $_.IPAddressToString }
Upvotes: 1
Reputation: 5140
Without access to get-netipaddress, here is a very dirty and long one liner. Change out "Local Area Connection" for your network's name.
((([System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ? {$_.Name -eq "Local Area Connection"}).GetIPProperties() | Select-Object -ExpandProperty 'UniCastAddresses' | ? {$_.IPv4Mask -ne
"0.0.0.0"}) | Select Address).Address.IPAddressToString
With Get-netIpAddress, I suspect this would also work:
$ipa = (Get-NetIPAddress |
Where-Object { $_.IfIndex -eq 19 -and $_.InterfaceAlias -eq "LAN2" -and $_.AddressFamily -eq "IPv4" } | `
Select-Object { $_.IPAddress }).IPAddress
Upvotes: 1
Reputation: 2718
You can accomplish what you are looking for by first getting a list of all network adapters on the machine using get-wmiobject
, selecting the name you want, then piping it to get-netipaddress
and then selecting the IPAddress
property as shown below.
(Get-WmiObject win32_networkadapter | Where-Object {$_.name -eq "YOUR_NAME"} | Get-NetIPAddress).IPAddress
Upvotes: 0