How to configure the network interface to use a dynamic IP address (DHCP) in PowerShell
July 28, 2021
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
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
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 ("188.8.131.52","184.108.40.206")
- How to set IP address, netmask, default gateway and DNS for a network interface in PowerShell (97.6%)
- Why does VirtualBox lose connection when changing MAC address (SOLVED) (54.3%)
- Demote Domain Controllers in Windows Server in PowerShell and GUI (53.4%)
- How to clear DNS and other caches in Google Chrome (52.4%)
- How to fix ERR_NETWORK_CHANGED error (SOLVED) (51.2%)
- How to find out all DNS records of sites behind CloudFlare (RANDOM - 2.4%)