Let’s get the local IP version 4 address of the system using PowerShell.

We want the actual IPv4 address of the local system but not the localhost loopback address or the link-local address on the system.

Link-local address is valid only for communications on local link (subnetwork) that our system is connected to. Link-local addresses are usually 169.254.0.0/16 (169.254.0.0 through 169.254.255.255). 1

Localhost is a hostname that refers to the current computer used to access it. The name localhost is reserved for loopback purposes. The IPv4 loopback address is 127.0.0.1. 2

Get IPv4 addresses

Let’s begin by just enumerating the IPv4 addresses along with the localhost loopback and link-local addresses.

Get-NetIPAddress -AddressFamily IPv4

In the output, we want the 192.168.1.8 IPv4 address, but we also get the localhost loop back (127.0.0.1) which we don’t want.

IPAddress         : 192.168.1.8       <--- want this
InterfaceIndex    : 14
InterfaceAlias    : Ethernet
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Dhcp
SuffixOrigin      : Dhcp
AddressState      : Preferred
ValidLifetime     : 1.22:58:59
PreferredLifetime : 1.22:58:59
SkipAsSource      : False
PolicyStore       : ActiveStore

IPAddress         : 127.0.0.1          <--- don't want this
InterfaceIndex    : 1
InterfaceAlias    : Loopback Pseudo-Interface 1
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 8
PrefixOrigin      : WellKnown
SuffixOrigin      : WellKnown
AddressState      : Preferred
ValidLifetime     : Infinite ([TimeSpan]::MaxValue)
PreferredLifetime : Infinite ([TimeSpan]::MaxValue)
SkipAsSource      : False
PolicyStore       : ActiveStore

The localhost loopback and link-locals use PrefixOrigin as WellKnown. We can return all of the IPv4 that are not equal to PrefixOrigin as WellKnown:

PrefixOrigin      : WellKnown    <--- filter based on this
Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet | Where-Object PrefixOrigin -ne WellKnown
IPAddress         : 192.168.1.8       <--- want this
InterfaceIndex    : 14
InterfaceAlias    : Ethernet
AddressFamily     : IPv4
Type              : Unicast
PrefixLength      : 24
PrefixOrigin      : Dhcp
SuffixOrigin      : Dhcp
AddressState      : Preferred
ValidLifetime     : 1.23:00:31
PreferredLifetime : 1.23:00:31
SkipAsSource      : False
PolicyStore       : ActiveStore

If we just want the IPAddress all we need to do is do .IPAddress and put everything into a paranthesis:

(Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet* | Where-Object PrefixOrigin -ne WellKnown).IpAddress

Output:

192.168.1.8

Now that we have returned IPAddress we can grab it and put it into a variable:

$myIpAddress = (Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet | Where-Object PrefixOrigin -ne WellKnown).IPAddress

If you have more than one Ethernet adapter you can use Ethernet* instead of Ethernet:

$myIpAddresses = (Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet* | Where-Object PrefixOrigin -ne WellKnown).IPAddress

Bonus: Get IPv4 address using ipconfig and ripgrep

If we take a IPv4 regex such as '((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}' we can search ipconfig for our IPv4 address:

ipconfig | rg -i 'IPv4 Address.*:' | rg -i '((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}' --only-matching

Done! 🛜