Share via


Netsh DHCP example

Applies To: Windows Server 2003, Windows Server 2003 R2, Windows Server 2003 with SP1, Windows Server 2003 with SP2

Netsh DHCP example

You can use Netsh commands for Dynamic Host Configuration Protocol (DHCP) in batch files and other scripts to automate tasks. The following example batch file 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.

For more information, see Setting Up Scopes, Setting Up Options, and DHCP options.

For more information and a complete list of Netsh commands for DHCP, see Netsh commands for DHCP.

In the following example procedure, 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.