Powershell - Managing IP configuration of network interfaces

To support me, you can subscribe to the channel, share and like the videos, disable your ad blocker or make a donation. Thank you!

Following on from the previous article on commands for viewing and changing the status and configuration of network interfaces, here is a set of commands for viewing and changing the IP settings of network interfaces.

# Display IP configuration
Get-NetIPConfiguration
# Displays the IP configuration of all interfaces, even disconnected ones
Get-NetIPConfiguration -All
# Specify an interface via its index
Get-NetIPConfiguration -InterfaceIndex 12 | fl *
# Specify an interface via its name
Get-NetIPConfiguration -InterfaceAlias Ethernet
# Display more information about the IP configuration
Get-NetIPConfiguration -Detailed
# Define a fixed IPv4, mask and default gateway
New-NetIPAddress 192.168.0.1 -PrefixLength 24 -InterfaceAlias Ethernet -DefaultGateway 192.168.0.254
# Note: By default, the IP is stored in two locations:
# - ActiveStore: Configuration used until the computer is shut down
# - PersistentStore: Configuration loaded (in the Active store) when the computer is started.
New-NetIPAddress 192.168.0.1 -PrefixLength 24 -InterfaceAlias Ethernet -DefaultGateway 192.168.0.254 -PolicyStore ActiveStore
# If you don't want it to appear in the DNS
# and not be used as the main address, add -SkipAsSource $True
New-NetIPAddress -InterfaceAlias ethernet -IPAddress 10.0.0.201 -PrefixLength 24 -AddressFamily IPv4 -SkipAsSource $True
# Define DNS
Set-DnsClientServerAddress -InterfaceAlias Ethernet -ServerAddresses 192.168.0.254,192.168.0.253
# Display DNS configuration
Get-DnsClientServerAddress
# Redefines DNS with default values (those of DHCP)
Set-DnsClientServerAddress -InterfaceAlias Ethernet -ResetServerAddresses
# Set a fixed IPv6, prefix and default gateway
New-NetIPAddress -InterfaceAlias Ethernet -IPAddress FD00::2 -PrefixLength 64 -DefaultGateway FD00::1
# Enable DHCP on a network interface
Set-NetIPInterface -InterfaceAlias Ethernet -Dhcp Enabled
# Note: this also removes the fixed IP but not the gateway
# remove the gateway
Remove-NetRoute -InterfaceAlias Ethernet -NextHop 192.168.0.254
# Disable DHCP on a network interface
Set-NetIPInterface -InterfaceAlias Ethernet -Dhcp Disabled
# Note: if you set a fixed IP, DHCP is automatically disabled.
# Display interface metrics, MTU and status for IPv4
Get-NetIPInterface -AddressFamily IPv4
# Or IPv6
Get-NetIPInterface -AddressFamily IPv6
# Modify the metric of an interface
Set-NetIPInterface -InterfaceAlias Ethernet -InterfaceMetric 20
# Display the IPv4 addresses configured for an interface
Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet |
Select-Object InterfaceAlias, IPAddress, PrefixLength
# Delete an IP without requesting confirmation
Get-NetIPAddress -AddressFamily IPv4 -InterfaceAlias Ethernet |
Remove-NetIPAddress -AddressFamily IPv4 -Confirm:$false
# Display global IPv4 protocol configuration
Get-NetIPv4Protocol | Select-Object -Property *
# Display global IPv6 protocol configuration
Get-NetIPv6Protocol | Select-Object -Property *
# It is of course possible to modify the global configuration of IPv4 and IPv6 protocols using the following commands
Set-NetIPv4Protocol
# or for IPv6
Set-NetIPv6Protocol

Related links