Get and set IP addresses with PowerShell

In my old school ways, getting the IP address of the local Windows machine was easy:

1
ipconfig

Now we have a more modern shell we can use that to see what addresses are currently assigned to a Windows machine. We can query the network adapter information directly from PowerShell.

1
Get-NetIPAddress

This gives you the core IP details for each adapter, but it is a little dense, so formatting the output into a table makes it much easier to read.

1
2
Get-NetIPAddress |
 Format-Table InterfaceAlias, InterfaceIndex, IPAddress, AddressFamily

If a device is not receiving a DHCP lease, Windows often falls back to an Automatic Private IP Address in the 169.254.0.0/16 range. That is a useful clue when you are troubleshooting connectivity issues.

1
2
3
4
Get-NetIPAddress |
 Where IPAddress -like "169.*" |
 Where AddressFamily -like IPv4 |
 Format-Table InterfaceAlias, InterfaceIndex, IPAddress, AddressFamily

When you need to assign a static address manually, New-NetIPAddress is the command to use. This example sets a typical IPv4 configuration with a gateway and a /24 subnet mask.

1
New-NetIPAddress -IPAddress 192.168.1.16 -PrefixLength 24 -DefaultGateway 192.168.1.1 -Confirm:$false

For the original version of this post see Andrew Davidson's personal blog at Get and set IP addresses with PowerShell