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 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.