Tag: Remove-NetRoute

How to configure the network interface to use a dynamic IP address (DHCP) in PowerShell

Note: all settings in this article must be done with administrator rights.

Dynamic Host Configuration Protocol (DHCP) allows the network adapter to obtain the correct network settings without manually configuring network interfaces.

To manage a network interface, you need to know its index. The list of interfaces and their indices can be obtained with the following command:

Get-NetIPAddress | Format-Table

Before activating DHCP on the network interface, you need to delete the IP address settings (if any).

Remove the static IP address:

Remove-NetIPAddress -InterfaceIndex INTERFACE_INDEX

Remove the default gateway:

Remove-NetRoute -InterfaceIndex INTERFACE_INDEX

For example:

Remove-NetIPAddress -InterfaceIndex 18
Remove-NetRoute -InterfaceIndex 18

Remove an IP address using a pipeline:

Get-NetIPAddress -IPAddress 192.168.0.1 | Remove-NetIPAddress

This command removes all of the IP addresses with the address 192.168.0.1.

To enable DHCP on the network interface, use a command of the form:

Set-NetIPInterface -InterfaceIndex INTERFACE_INDEX -Dhcp Enabled

For example:

Set-NetIPInterface -InterfaceIndex 18 -Dhcp Enabled

If you want to delete records about DNS servers so that DNS settings are also obtained automatically, then run the following command:

Set-DnsClientServerAddress -InterfaceIndex INTERFACE_INDEX -ResetServerAddresses

Please note that you can use DHCP and DNS server settings at the same time (specify the desired DNS servers for the network interface, which automatically receive an IP address and other network settings). 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 ("8.8.8.8","8.8.4.4")

How to set IP address, netmask, default gateway and DNS for a network interface in PowerShell

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:

Get-NetIPAddress

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 ("8.8.8.8","8.8.4.4")
Loading...
X