How to get IP address settings using PowerShell?


To get the IP address of the system we can use IPConfig command in cmd and the same command can be used in PowerShell. IPConfig command shows all the connected and disconnected adapters including IPv4 and IPv6. For example,

Example

PS C:\Users\Administrator> Ipconfig

Windows IP Configuration

Ethernet adapter Ethernet0:

   Connection-specific DNS Suffix  . :
   IPv4 Address. . . . . . . . . . . : 192.168.0.104
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1

Tunnel adapter isatap.{27E40122-664A-404D-A4C9-4E48C0363BC5}:

   Media State . . . . . . . . . . . : Media disconnected
   Connection-specific DNS Suffix  . :

Tunnel adapter Local Area Connection* 3:

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::2ca8:29d5:3f57:ff97%5
   Default Gateway . . . . . . . . . :

But the problem with this utility is if you need to filter specific property like adapter name or the IP family (IPv4 or IPv6), string operation is required to filter the result out and this takes a bunch of lines of code to write. PowerShell also supports the similar command Get-NetIPAddress.

Get-NetIPAddress | ft -AutoSize

Output

To retrieve only IPv4 addresses,

Get-NetIPAddress -AddressFamily IPv4 | ft -AutoSize

Output

Similarly, you can retrieve IPv6 addresses.

Get-NetIPAddress -AddressFamily IPv6 | ft -AutoSize

You can also check if the IP is through DHCP or configured manually. Check the PrefixOrigin and SuffixOrigin properties in the above IPv4 example and that is DHCP. If you have static IP configured then the same will be reflected in both the properties. For Example,

To retrieve the IP address for the specific interface, use -InterfaceIndex parameter. For example,

Example

Get-NetIPAddress -InterfaceIndex 3 | ft -AutoSize

Output

To get the IP address settings for the remote computer, there is -CimSession parameter supported.

$sess = New-CimSession -ComputerName Test1-win2k16
Get-NetIPAddress -CimSession $sess | ft -AutoSize

Output

You can apply the other parameters to filter out the result as mentioned in the above examples.

Updated on: 11-Nov-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements