Skip to main content

How to Set a Dynamic (DHCP) IP Address in a Batch Script

In a standard network environment, most computers are configured to obtain an IP address automatically from a DHCP (Dynamic Host Configuration Protocol) server. This simplifies network management immensely. However, there are times, specially during network troubleshooting or in a scripting environment, when you might need to switch a network adapter from a static (manually assigned) IP address back to the dynamic DHCP configuration.

This guide will teach you how to use the powerful, built-in netsh (Network Shell) command to configure a network adapter to obtain its IP address and DNS server addresses automatically from DHCP. You will learn the correct commands and the importance of running the script with administrator privileges.

The Core Command: netsh (Network Shell)

The netsh.exe command is the primary command-line utility for managing almost every aspect of a computer's network configuration, including interfaces, IP addresses, and firewall settings.

To change IP settings, we use the interface ip context of netsh.

danger

CRITICAL WARNING: Changing your computer's IP address will cause a brief network disconnection. This operation is a system-level change and must be run with full administrator privileges.

Step 1: Finding the Exact Network Interface Name

Before you can change the settings for a network adapter, you must know its exact name. These are the names you see in the Network Connections control panel (e.g., "Ethernet", "Wi-Fi").

Command to Get the Names: netsh interface show interface

This command lists all the network interfaces on your system. You must use a name from the "Interface Name" column.

Admin State    State          Type             Interface Name
-------------------------------------------------------------------------
Enabled Connected Dedicated Ethernet
Enabled Connected Dedicated Wi-Fi

In this case, the names are Ethernet and Wi-Fi.

Step 2: The Commands to Enable DHCP

Switching back to a dynamic configuration is a two-step process:

  1. Set the IP address configuration to DHCP.
  2. Set the DNS server configuration to DHCP.

Command to Enable DHCP for the IP Address

netsh interface ip set address name="Interface Name" source=dhcp
  • name="Interface Name": The name of the adapter you identified in Step 1.
  • source=dhcp: This is the key part that tells the adapter to get its IP address automatically.

Command to Enable DHCP for DNS

netsh interface ip set dns name="Interface Name" source=dhcp

You must run both commands to fully revert to a dynamic configuration.

The Full Script: Enabling DHCP for a Specific Adapter

This script will switch an adapter named "Ethernet" from a static IP back to DHCP.

@ECHO OFF
REM This script MUST be run as an Administrator.

SET "AdapterName=Ethernet"

ECHO --- Configuring "%AdapterName%" for DHCP ---
ECHO.
ECHO Step 1: Setting IP address source to DHCP...
netsh interface ip set address name="%AdapterName%" source=dhcp

ECHO.
ECHO Step 2: Setting DNS server source to DHCP...
netsh interface ip set dns name="%AdapterName%" source=dhcp

ECHO.
ECHO --- Configuration complete ---
ECHO Running ipconfig to verify...
ipconfig /all | find "%AdapterName%" -A 10
note

The ipconfig command at the end is used to display the new settings for verification.

Common Pitfalls and How to Solve Them

  • Not Running as Administrator: This is the most common reason for failure. netsh will fail with an "Access is denied" or "The requested operation requires elevation" error. Solution: Always run the script from an elevated ("Run as Administrator") command prompt.

  • Incorrect Interface Name: The name provided in the name="..." parameter must be an exact match for the name shown by netsh interface show interface.

    • Solution: Before running the script, use netsh interface show interface to get the correct name. Copy and paste it to avoid typos.
  • Brief Network Interruption: When you change the IP address settings, the network adapter will reset. This will cause a brief disconnection from the network, which is normal and expected.

Practical Example: A Network Reset Script

This script is a useful troubleshooting tool. It provides a menu to either set a static IP address for testing or to revert back to the standard DHCP configuration.

@ECHO OFF
SETLOCAL
REM This script MUST be run as an Administrator.

SET "AdapterName=Ethernet"

:Menu
CLS
ECHO --- Network Configuration Utility ---
ECHO Adapter: %AdapterName%
ECHO =======================================
ECHO 1. Set a STATIC IP Address (192.168.1.200)
ECHO 2. Set a DYNAMIC IP Address (DHCP)
ECHO 3. Exit
ECHO.

CHOICE /C 123 /M "Please select an option: "

IF ERRORLEVEL 3 GOTO :EOF
IF ERRORLEVEL 2 GOTO :SetDynamic
IF ERRORLEVEL 1 GOTO :SetStatic

:SetStatic
ECHO Setting static IP address...
netsh interface ip set address name="%AdapterName%" static 192.168.1.200 255.255.255.0 192.168.1.1
netsh interface ip set dns name="%AdapterName%" static 8.8.8.8
GOTO :Finish

:SetDynamic
ECHO Setting dynamic (DHCP) IP address...
netsh interface ip set address name="%AdapterName%" source=dhcp
netsh interface ip set dns name="%AdapterName%" source=dhcp
GOTO :Finish

:Finish
ECHO.
ECHO Configuration applied. Displaying new settings...
ipconfig | find "%AdapterName%" -A 5
PAUSE
GOTO :Menu

Conclusion

The netsh command is the definitive and most powerful tool for programmatically managing network adapter settings in a batch script.

For a successful DHCP configuration:

  1. Always run your script as an Administrator.
  2. First, use netsh interface show interface to get the exact name of the adapter you want to configure.
  3. Run both of the core commands:
    • netsh interface ip set address name="..." source=dhcp
    • netsh interface ip set dns name="..." source=dhcp

By using netsh, you can fully automate the process of reconfiguring a network adapter, making it a crucial tool for network troubleshooting and management scripts.