How to get IP address settings using PowerShell?

PowerShellMicrosoft TechnologiesSoftware & Coding

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

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

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

Tunnel adapter Local Area Connection* 3:

Connection-specific DNS Suffix  . :
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

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.

Published on 11-Nov-2020 17:23:38