Get IPv4 Address in PowerShell

Get IPv4 Address in PowerShell

  1. Use Get-NetIPAddress to Get IPv4 Address Into a Variable in PowerShell
  2. Use Get-NetIPConfiguration to Get IPv4 Address Into a Variable in PowerShell
  3. Use Dns class to Get IPv4 Address Into a Variable in PowerShell
  4. Use Get-WmiObject to Get IPv4 Address Into a Variable in PowerShell
  5. Use Test-Connection to Get IPv4 Address Into a Variable in PowerShell

System administrators can perform various tasks in PowerShell, including networking tasks.

For example, PowerShell allows you to list IP addresses and IP configuration, ping a computer, retrieve network adapter properties, perform DHCP configuration tasks, and create/remove a network share.

IP means Internet Protocol, and v4 means Version Four.

IPv4 address is 32-bit integers that identify a network interface on a machine. It is expressed in dotted-decimal notation.

It has four 8-bit fields separated by periods, and each 8-bit field represents a byte of the IPv4 address. This tutorial will teach you to get an IPv4 address into a variable in PowerShell.

Use Get-NetIPAddress to Get IPv4 Address Into a Variable in PowerShell

The Get-NetIPAddress cmdlet gets the IP address configuration, such as IPv4 addresses, IPv6 addresses, and the IP interfaces associated with addresses.

When it is run without parameters, it prints the entire IP address configuration for the computer.

The --AddressFamily parameter is used to specify an array of IP address families. The Get-NetIPAddress gets the IP address configuration that matches the address families with this parameter.

The accepted values are IPv4 and IPv6.

For example, the command below gets information about IP address configuration for all IPv4 addresses on the computer.

Get-NetIPAddress -AddressFamily IPv4

This command gets only the computer’s IPv4 address into a variable $ipv4.

$ipv4 = (Get-NetIPAddress | Where-Object {$_.AddressState -eq "Preferred" -and $_.ValidLifetime -lt "24:00:00"}).IPAddress
$ipv4

Output:

192.168.2.33

You can also use the following command to get an IPv4 address in PowerShell.

Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile | Select-Object -ExpandProperty InterfaceIndex) | Select-Object -ExpandProperty IPAddress

Output:

192.168.2.33

Use Get-NetIPConfiguration to Get IPv4 Address Into a Variable in PowerShell

The Get-NetIPConfiguration cmdlet gets network configuration, including IP addresses and DNS servers.

This cmdlet gets IP configuration properties for all non-virtual connected interfaces on a computer when run without any parameters.

The following command gets an IPv4 address into a variable $ipv4.

$ipv4 = (Get-NetIPConfiguration | Where-Object {$_.IPv4DefaultGateway -ne $null -and $_.NetAdapter.status -ne "Disconnected"}).IPv4Address.IPAddress
$ipv4

Output:

192.168.2.33

Use Dns class to Get IPv4 Address Into a Variable in PowerShell

The Dns class is a static class that gets information about a specific host from the Internet Domain Name System (DNS). The GetHostAddresses method returns the IP addresses for the specified host.

For example, the following command gets the IPv4 address for LAPTOP-B045GB7Y, which is the device name for this computer. You have to replace it with your device name to get the IPv4 address of your computer.

$ipv4 = ([System.Net.DNS]::GetHostAddresses('LAPTOP-B045GB7Y') | Where-Object {$_.AddressFamily -eq "InterNetwork"} | select-object IPAddressToString)[0].IPAddressToString
$ipv4

Output:

192.168.2.33

Use Get-WmiObject to Get IPv4 Address Into a Variable in PowerShell

The Get-WmiObject cmdlet gets instances of Windows Management Instrumentation (WMI) classes or information about the available WMI classes.

The Win32_NetworkAdapterConfiguration is a WMI class that represents the attributes and behaviors of a network adapter.

You can run the following command to get an IPv4 address into a variable $ipaddr in PowerShell.

$ipaddr = (Get-WmiObject -Class Win32_NetworkAdapterConfiguration | where {$_.DHCPEnabled -ne $null -and $_.DefaultIPGateway -ne $null}).IPAddress | Select-Object -First 1
$ipaddr

Output:

192.168.2.33

Use Test-Connection to Get IPv4 Address Into a Variable in PowerShell

The Test-Connection cmdlet sends ICMP echo request packets, or pings, to one or more computers and returns the echo response replies. It can also determine whether a specific computer can be contacted across an IP network.

You can run the following command to get an IPv4 address into a variable $ipv4.

$ipv4 = (Test-Connection -ComputerName (hostname) -Count 1 | Select -ExpandProperty IPv4Address).IPAddressToString
$ipv4

If you have any virtual adapters, it returns an IPv4 address of virtual adapters (e.g., VPN, Hyper-V, Docker).

Output:

192.168.26.1

You can also use this command which returns the same output as above.

(Test-Connection -ComputerName $env:computername -count 1).IPv4Address.IPAddressToString

Output:

192.168.26.1
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.