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
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")
Related articles:
- How to set IP address, netmask, default gateway and DNS for a network interface in PowerShell (97.9%)
- Why does VirtualBox lose connection when changing MAC address (SOLVED) (53.7%)
- Demote Domain Controllers in Windows Server in PowerShell and GUI (52.5%)
- How to clear DNS and other caches in Google Chrome (52.1%)
- How to find out all DNS records of sites behind CloudFlare (52.1%)
- Error “ERROR 1142 (42000)”: command denied to user for table in MySQL / MariaDB (SOLVED) (RANDOM - 50%)