Use PowerShell to Recreate DHCP Scopes

Use Windows PowerShell to recreate DHCP scopes.

John Savill

April 14, 2014

1 Min Read
scope

Q: Is there an easy way to create a new DHCP scope and options through PowerShell?

A: I recently had to delete a bunch of DHCP scopes, then recreate them and enable failover replication. Because I wanted to do them one at a time, I created the following basic PowerShell script.

$3rdOctet = 5Remove-DhcpServerv4Scope -ScopeId 10.0.$3rdOctet.0 -ForceAdd-DhcpServerv4Scope -EndRange 10.0.$3rdOctet.200 -Name Scope-$3rdOctet -StartRange 10.0.$3rdOctet.50 -SubnetMask 255.255.255.0 -LeaseDuration 8.0:0:0 -State Active -Type DhcpSet-DhcpServerv4OptionValue -ScopeId 10.0.$3rdOctet.0 -OptionId 3 -Value 10.0.$3rdOctet.1Set-DhcpServerv4OptionValue -ScopeId 10.0.$3rdOctet.0 -OptionId 6 -Value 10.0.$3rdOctet.10,10.1.1.10 -ForceAdd-DhcpServerv4Failover -ScopeId 10.0.$3rdOctet.0 -PartnerServer DHCP02 -Name TVLAN-$3rdOctetGet-DhcpServerv4Failover -ScopeId 10.0.$3rdOctet.0

Note that in my environment all the scopes are 10.0..0 (e.g., 10.0.1.0, 10.0.2.0, and so on). They also use one DHCP server from the scope, 10.0..10, and a general DNS server, 10.1.1.10. Addresses 50 through 200 are used for the actual scope. You might want to modify the actual values for your environment. Just change the 3rdOctet variable at the start for the specific scope you want to delete, create, then enable for replication.

The replica server in my environment is DHCP02; you'd need to change it to the name of your partner DHCP server. My script doesn't capture any current leases; therefore, you should ensure that IPv4 is configured to test for an actual IP use of at least one try before allocating. You can configure this on the IPv4 properties Advanced tab as Conflict detection attempts. This means that the server will try to contact an IP address before allocating it. If there's no response, the server will go ahead and lease the IP address.

About the Author(s)

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like