PowerShell DHCP Scope in Windows Server 2012

PowerShell v3 Add-DhcpServerv4Scope Powershell DHCP Scope in Windows Server 2012

In Windows Server 2012 you can employ PowerShell to configure DHCP.  While there are dozens of PowerShell DHCP functions (cmdlets), on this page I concentrate on the aptly named DhcpServerv4Scope.

 ♦

Basics of Creating a DHCP Scope

When you assign IPv4 and IPv6 addresses via a DHCP server the key concept is the scope.  The minimum four properties to specify are: a name for the scope, the starting and ending IP address of the range, and the subnet mask.  For the sake of getting started I will gloss over the numerous optional properties.

How to Create a DHCP Scope with PowerShell

# PowerShell 3.0 creates a DHCP Scope
Add-DHCPServerv4Scope
-Name Guy`
-StartRange 192.168.2.170`
-EndRange 192.168.2.179`
-SubnetMask 255.255.255.0

Note 1: I needed PowerShell's backtick (`) because I wanted to show each parameter (property) on its own line.Add-DHCPServerV4Scope PowerShell DHCP Scope

Check your new scope
I recommend that you compare:

a) The results of PowerShell's:

Get-DHCPServerv4Scope

with

b) What you see in the DHCP Server's GUI, see screenshot above right.

Windows Server 12 DHCP Server's GUI

Set-DHCPServerV4Scope 

Guy Recommends 3 Free Active Directory ToolsDownload Solarwinds Active Directory Administration Tool

SolarWinds have produced three Active Directory add-ons.  These free utilities have been approved by Microsoft, and will help to manage your domain by:

  1. Seeking and zapping unwanted user accounts.
  2. Finding inactive computers.
  3. Bulk-importing new users.  Give this AD utility a try, it’s free!

Download your FREE Active Directory administration tools.

Researching PowerShell DHCP Functions (Cmdlets)

I want to concentrate on the DHCPServerV4 commands, note that 'V', and be aware of the parallel IP V6 commands.

# Research PowerShell's DHCP Cmdlets
Get-command Add-DhcpServerv4*

Resulting Functions (Cmdlets):

Function Add-DhcpServerv4Class 
Function Add-DhcpServerv4ExclusionRange 
Function Add-DhcpServerv4Failover 
Function Add-DhcpServerv4FailoverScope 
Function Add-DhcpServerv4Filter 
Function Add-DhcpServerv4Lease 
Function Add-DhcpServerv4OptionDefinition 
Function Add-DhcpServerv4Policy 
Function Add-DhcpServerv4PolicyIPRange 
Function Add-DhcpServerv4Reservation 
Function Add-DhcpServerv4Scope 

It's taking a while to get used to the concept that Add-DhcpServerv4Scope, and similar Windows Server 12 PowerShell commands, are not actually cmdlets, but functions.

Challenge 1: Repeat, but substitute 'Get' for 'Add'.

Challenge 2: Go the whole nine yards, substitute '*' for 'Add'.

Edit Your DHCP Scope with Set-DhcpServerv4Scope

Configuring DHCP through PowerShell looks easy; after all there are dozens of cmdlets, or to give them their proper term -functions.  However, the secret of success is to pay close attention to the ScopeId property.  In this example its value is precisely: 192.168.1.0

# PowerShell sets DHCP lease to 9 days.
Set-DhcpServerv4Scope -ScopeId 192.168.1.0 -LeaseDuration (New-TimeSpan -Days 9)

Note 2:  -ScopeId took me ages to figure out.  My problem was overthink, no need for any brackets or speech marks, just the plain IP address.  Remember that zero at the end and forget about appending the name of the scope.

Note 3: The syntax of -LeaseDuration, or -Lease if you prefer, cause me a few headaches.  Just study my timespan example and amend Days 9 as needed.

Research DHCP Properties or Parameters

Let us assume that you need to add information to the DHCP scope, for example, to set a description, or to change the 'State' from inactive to active.  To research parameter I recommend that you prefix the PowerShell DHCP function with Get-Help.

# Research PowerShell's DHCP parameters
Get-Help Set-DhcpServerv4Scope

Note 4: This reveals new parameters such as -State and -Description.  It also reminds us of old friends such as -LeaseDuration and -EndRange.

Note 5: You see pretty much the same parameters if substitute the verbs, Get or Remove for Set.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v12 v12

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

Perhaps the NPM’s best feature is the way it suggests solutions to network problems.  Its second best feature is the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you give this Network Performance Monitor a try.

Download your free trial of SolarWinds Network Performance Monitor.

Remove a DHCP Scope with PowerShell

My use for this technique was in testing.  In truth I got in a right old stew before I discovered the simple and elegant:

Remove-DhcpServerv4Scope -ScopeId 192.168.1.0

Note 6: For consistency PowerShell always uses the Remove verb, and never delete or kill.

Out-takes

The following shows the thought processes of a demented PowerShell scripter.

Clear-host
Restart-Service dhcpserver
$Scopy = (Get-DhcpServerv4Scope -ScopeId 192.168.1.0).ScopeId.IPAddressToString
$Scopy
Remove-DhcpServerv4Scope -ScopeId $Scopy

Note 7: This example proved to me that the value of the ScopeId was a pure dot decimal number 192.168.1.0, and not [192.168.1.0 Guy] or other even more bizarre ideas.

See how PowerShell can add features to Windows Server 2012 »

Summary of Add-DhcpServerv4Scope in Windows Server 2012

How PowerShell's DHCP scope functions (cmdlets) can configure Windows Server 2012. Examples of Add and Set-DhcpServerv4Scope explain how to create the scope's range and set the lease.

If you like this page then please share it with your friends

 


Microsoft Windows Server 2012 Topics

Windows Server 2012 Home   • Install Windows Server 2012   • Windows Server 2012 PowerShell

PowerShell Add-WindowsFeature   • Windows Server 2012 Hyper-V  • Disable IE ESC Server 2012

Shutdown Windows Server 2012   • PowerShell DHCP Scope   • Network Performance Monitor