Powershell

PowerShell Get IPv4 Address Into a Variable

PowerShell is a Windows administrator tool used to perform several automation tasks, including tasks related to networking. System administrators usually need to work with the IPv4 addresses. (IPv4) Internet Protocol Version 4 is the fourth version of the (IP) internet protocol. It is a 32-bit integer address representing a network interface on the system. More specifically, the IPv4 address can be retrieved into a variable in PowerShell.

This write-up will observe various methods to retrieve the IPv4 address.

How to Get/Retrieve IPv4 Address Into a PowerShell Variable?

These cmdlets can be used to retrieve the IPv4 address into a variable:

Method 1: Get IPv4 Address Into a Variable Using “Test-Connection” Command

The “Test-Connection” cmdlet, with the combination of the other parameters and commands, can get an IPv4 address into a variable. It is also used to send echo requests or packets to other computers and, in return, gets the echo replies.

Example

Execute the given command to get and store the IPv4 address into a variable:

> $Var1 = Test-Connection -ComputerName localhost -Count 1 | Select IPV4Address

Now, invoke the variable in which the IPv4 address has been stored:

It can be observed that the mentioned variable contains the IPv4 address.

Method 2: Get IPv4 Address Into a Variable Using “IPConfig” Command

Another command that is used to get the IPv4 address into a variable is the “IPConfig” command. It can be utilized to output the current network configurations, such as TCP/IP.

Example

Run the given command to get the IPv4 address into a variable:

> $IPadr =((ipconfig | findstr [0-9].\.)[0]).Split()[-1]

Now, verify the stored IP address value:

> $IPadr

Method 3: Get IPv4 Address Into a Variable Using “Get-NetIPConfiguration” Command

The “Get-NetIPConfiguration” command is used to display network configurations, such as DNS servers and IP addresses. This command can also assist in getting the IPv4 address into a variable.

Example

Run the given line of code to get the IPv4 into a variable using the “Get-NetIPConfiguration” cmdlet:

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

Verification

> $val1

Method 4: Get IPv4 Address Into a Variable Using “Get-WmiObject” Command

The “Get-WmiObject” cmdlet is used to retrieve the “Windows Management Instances(WMI)” class’s instances. More specifically, the “Win32_NetworkAdapterConfiguration” is a “WMI” class represents the network’s attributes and behaviors.

Example

Execute the below mentioned command to store the IPv4 address into a variable:

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

Verification

> $val2

Method 5: Get IPv4 Address Into a Variable Using “Get-NetIPAddress” Command

The “Get-NetIPAddress” cmdlet is utilized to retrieve the IP configuration, such as IPv4. It is typically used to get the IP addresses, such as IPv6 and IPv6. Moreover, it also accesses the associated IP addresses.

Example

For the demonstration, run the following command:

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

Verification

> $val3

It can be seen that the IPv4 address has been displayed in the output.

Conclusion

In PowerShell, the IPv4 address can be retrieved into a variable using several cmdlets. These cmdlets include “Test-Connection”, “IPConfig”, “Get-NetIPConfiguration”, “Get-WmiObject”, and the “Get-NetIPAddress”.This write-up has elaborated multiple methods to get the IPv4 address into a variable.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.