Netsh DHCP Example
Applies To: Windows Server 2008
You can use Netsh commands for Dynamic Host Configuration Protocol (DHCP) in batch files and other scripts to automate tasks. The following batch file example demonstrates how to use Netsh commands for DHCP to perform a variety of related tasks.
In the circumstance of this example procedure, DHCP-01 is a DHCP server with the IP address 192.168.0.2. The procedure adds a new scope to DHCP-01 with the name MyScope, IP address 192.168.10.0, subnet mask 255.255.255.0, and comment NewScope. It then configures the scope with an address range (192.168.10.1 through 192.168.10.254), an exclusion range (192.168.10.1 through 192.168.10.25), and router IP addresses (DHCP option 003). The scope is then set to an active state.
In the following procedure example, lines that contain comments are preceded by "rem," for remark. Netsh ignores comments.
rem one DHCP server:
rem (DHCP-01) 192.168.0.2
rem 1. Connect to (DHCP-01), and add the scope MyScope with IP address 192.168.10.0,
rem 1.1 subnet mask 255.255.255.0, and the comment NewScope.
netsh dhcp server 192.168.0.2 add scope 192.168.10.0 255.255.255.0 MyScope NewScope
rem 2. Connect to (DHCP-01 MyScope), and add IP address range 192.168.10.1 to 192.168.10.254 for distribution
rem 2.1 and the default ClientType of DHCP.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 add iprange 192.168.10.1 192.168.10.254
rem 3. Connect to (DHCP-01 MyScope), and add IP exclusion range 192.168.10.1 to 192.168.10.25
rem 3.1 and the default ClientType of DHCP.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 add excluderange 192.168.10.1 192.168.10.25
rem 4. Connect to (DHCP-01 MyScope), and set the value of option code 003
rem 4.1 to list two router IP addresses (10.1.1.1, 10.1.1.2).
netsh dhcp server 192.168.0.2 scope 192.168.10.0 set optionvalue 003 IPADDRESS 10.1.1.1 10.1.1.2
rem 5. Connect to (DHCP-01 MyScope), and set the scope state to active.
netsh dhcp server 192.168.0.2 scope 192.168.10.0 set state 1
rem 6. End example batch file.
The following table lists the netsh dhcp commands that are used in this example procedure.
Command | Description |
---|---|
server |
Shifts the current DHCP command-line context to the server that is specified by either its name or IP address. |
add scope |
Adds a new scope to the specified DHCP server. |
scope |
Switches the command context to the DHCP scope that is specified by its IP address. |
add iprange |
Adds a range of IP addresses to the current scope. |
add excluderange |
Adds a range of excluded addresses to the current scope. |
set optionvalue |
Sets an option value for the current scope. |
set state |
Sets or resets the state of the current scope to either an active or inactive state. |