How to set IP address, netmask, default gateway and DNS for a network interface in PowerShell
July 28, 2021
Setting a static IP, netmask and gateway for a network adapter in PowerShell is a little confusing but possible. This post will show you how to do this using the New-NetIPAddress and Set-NetIPAddress cmdlets.
The old way of configuring network interfaces is using the netsh command. First, we look at the name of the network interfaces of any of the following commands (they are equivalent):
netsh interface show interface netsh int show int
Then we set the parameters for the selected network interface (in the following commands this is the interface named “Wi-Fi”):
netsh interface ip set address "Wi-Fi" static <IP> <SUBNET> <GATEWATE> netsh interface ip set dns "Wi-Fi" static <DNS SERVER>
But the netsh command is deprecated and Microsoft warns that they can remove netsh and we should use Powershell.
To manage a network adapter, we need to know its index, you can get it with the command:
The Get-NetIPAddress cmdlet retrieves the IP address configuration such as IPv4 addresses, IPv6 addresses, and IP interfaces to which the addresses are associated.
You can use the following command to present information more conveniently:
Get-NetIPAddress | Format-Table
Additionally, you can sort by interface index number:
Get-NetIPAddress | Sort-Object -Property InterfaceIndex | Format-Table
Then, using the New-NetIPAddress cmdlet, you can set a static IP address:
New-NetIPAddress -InterfaceIndex NTERFACE_INDEX -IPAddress IP_ADDRESS -PrefixLength MASK -DefaultGateway GATEWAY
For example, the command to configure the network adapter with index 18 static IP address 192.168.1.71 with subnet mask /24 (255.255.255.0) and default gateway 192.168.1.1:
New-NetIPAddress -InterfaceIndex 18 -IPAddress 192.168.1.71 -PrefixLength 24 -DefaultGateway 192.168.1.1
This command will work fine if you want to switch from a dynamic IP address to a static one. But if you want to change the parameters of the network interface on which a static IP address is already set, then you will receive the following error:
New-NetIPAddress: Instance DefaultGateway already exists
To work around this, you can use Set-NetIPAddress, but this cmdlet does not accept the -DefaultGateway parameter, so a new address is assigned, but with the old gateway.
For example, the following command:
Set-NetIPAddress -InterfaceIndex 18 -IPAddress 192.168.1.71 -PrefixLength 24 -DefaultGateway 192.168.1.1
fails with the error:
Set-NetIPAddress: A parameter cannot be found that matches parameter name 'DefaultGateway'.
How do I remove the gateway to start with New-NetIPAddress, or replace the gateway when using Set-NetIPAddress?
It is necessary to use the following algorithm of actions.
Remove the static IP address:
Remove-NetIPAddress -InterfaceIndex INTERFACE_INDEX
Removing the default gateway:
Remove-NetRoute -InterfaceIndex INTERFACE_INDEX
Add a new IP address and gateway:
New-NetIPAddress -InterfaceIndex INTERFACE_INDEX -AddressFamily IPv4 10.1.2.4 -PrefixLength 24 -Type Unicast -DefaultGateway 10.1.2.255
To set the DNS server settings on the network interface with the specified index, use a command of the form:
Set-DnsClientServerAddress -InterfaceIndex INTERFACE_INDEX -ServerAddresses ("18.104.22.168","22.214.171.124")
- How to configure the network interface to use a dynamic IP address (DHCP) in PowerShell (100%)
- Why does VirtualBox lose connection when changing MAC address (SOLVED) (54.5%)
- How to fix ERR_NETWORK_CHANGED error (SOLVED) (51.3%)
- How do I know if I am behind NAT? (SOLVED) (51.3%)
- How to check if my router supports IPv6 (51.3%)
- How to install Squid proxy on Windows (RANDOM - 50%)