Bulk load DHCP Reservations using DHCP PowerShell

Many organizations deploy DHCP server but choose to have complete control over the IP addresses given out to client computers. This is achieved by reserving an IP address for each client as opposed to letting them acquire a lease dynamically. If you are an admin at one such organization, you could probably have a long list of reservations to configure on the DHCP server. Doing so manually one at a time is a pain staking and error prone activity. The mechanism shown below allows you to add all of them in one go.

DHCP PowerShell introduced in Windows Server 2012 makes it very easy for admins to manage DHCP reservations.  Following are the DHCP PowerShell cmdlets which help manage reservations on the DHCP server.

Add-DhcpServerv4Reservation, Add-DhcpServerv6Reservation - Adds a DHCPv4 or DHCPv6 reservation to a DHCP server

Remove-DhcpServerv4Reservation, Remove-DhcpServerv6Reservation – Deletes DHCPv4 or DHCPv6 reservation(s) from a DHCP server

Get-DhcpServerv4Reservation, Get-DhcpServerv6Reservation – Gets DHCPv4 or DHCPv6 reservations from a DHCP server

Set-DhcpServerv4Reservation, Set-DhcpServerv6Reservation – Modifies the properties of a DHCPv4 or DHCPv6 reservation

If you want to add a large list of reservations, an input text file in CSV format can be used to provide the list of reservations to be configured on the DHCP server. This data can be easily pipelined to Add-DhcpServerv4Reservation cmdlet to add the complete list to the DHCP Server. The input text file (Reservations.csv in the command line used later) containing the reservations should be of the following format -

ScopeId,IPAddress,Name,ClientId,Description

10.10.10.0,10.10.10.10,Computer1,1a-1b-1c-1d-1e-1f,Reserved for Computer1

20.20.20.0,20.20.20.11,Computer2,2a-2b-2c-2d-2e-2f,Reserved for Computer2

30.30.30.0,30.30.30.12,Computer3,3a-3b-3c-3d-3e-3f,Reserved for Computer3

Note that the client id for most clients including Windows computers is the MAC address.

The following command adds all these reservations to the DHCP Server.

Import-Csv Reservations.csv | Add-DhcpServerv4Reservation

The Import-Csv cmdlet reads the CSV file and creates an array of reservations objects where an object is created from each row in the CSV file. This list of objects is passed to Add-DhcpServerv4Reservation cmdlet through pipeline which adds each of the reservations passed through the pipeline. An
important thing to note is that the column names in the CSV file are same as the parameter names of Add-DhcpServerv4Reservation cmdlet. This ensures that
each field of the reservation object gets specified as the corresponding parameter value when Add-DhcpServerv4Reservation is invoked.

While creating reservations for a list of clients you would want to reserve IP addresses which are not already leased out to clients. You can use Get-DhcpServerv4FreeIPAddress cmdlet to get a free IP address and reserve it for a client.. The cmdlet Get-DhcpServerv4FreeIPAddress returns a list of IP addresses which are not already leased out or reserved – neat, isn’t it. Here is an example which gets free IP addresses and reserves it for a list of clients.

The input text file (Clients.csv in the command line shown later) in this case should be of the following format -

ScopeId,Name,ClientId,Description

10.10.10.0,Computer1,1a-1b-1c-1d-1e-1f,Reserved for Computer1

20.20.20.0,Computer2,2a-2b-2c-2d-2e-2f,Reserved for Computer2

30.30.30.0,Computer3,3a-3b-3c-3d-3e-3f,Reserved for Computer3

The following command creates reservations for all the clients specified in Clients.csv. This example works in the same way as the earlier one except that for each client in Clients.csv, instead of the IP addressed to be reseved being specified in the file, the cmdlet Get-DhcpServerv4FreeIPAddress is invoked and the free IP address returned is used to add the reservation using Add-DhcpServerv4Reservation.

Import-Csv Clients.csv | %{ $FreeIp = Get-DhcpServerv4FreeIPAddress -Scopeid $_.ScopeId; $_ | Add-DhcpServerv4Reservation -IpAddress $FreeIp }

While these examples describe IPv4 scenarios, you could achieve the same in IPv6 as well.

Comments

  • Anonymous
    January 01, 2003
    The comment has been removed

  • Anonymous
    January 01, 2003
    what does the $_ mean after the $_.ScopeId;?thanks

  • Anonymous
    January 01, 2003
    Nice info.

  • Anonymous
    January 01, 2003
    Basayya, you can run this cmdlet remotely against a DHCP server running on Windows Server 2008 R2. The cmdlet needs to run from a Windows 8 client (with RSAT installed) or Windows Server 2012. See the blog post at blogs.technet.com/.../using-windows-8-dhcp-powershell-cmdlets-with-dhcp-server-running-on-windows-server-2008-or-windows-server-2008-r2.aspx

  • Anonymous
    January 01, 2003
    Yes, you can use this cmdlet with target DHCP server being Windows Server 2008, 2008 R2 and 2012. It will work with Windows 2008 SP2.

  • Anonymous
    January 28, 2013
    Can this cmdlet be used on a win 2008 sp2 servers?

  • Anonymous
    August 07, 2013
    Hi, Can i run this cmdlet in win 2008 r2?

  • Anonymous
    November 15, 2013
    I have configured some scopes to be reservation only but get many event log errors stating the scope is 100 percent full.  The service really seems to not like not having scopes that do not have a dynamic address pool to play with...  Is there a way to supress these errors for these scopes only?  I do want to be notified if another scope is running out of addresses and needs attention. Thanks

  • Anonymous
    November 15, 2013
    NSCC Tech, suppressing scope full events is not possible for reservation only scopes. Thanks for sharing the feedback.

  • Anonymous
    June 30, 2015
    With the Add-dhcpv4reservation command, can we also add the name not just a description?

  • Anonymous
    July 01, 2015
    Dan, yes you can add a name for the reservation. In the example in the blog, Computer1 through Computer3 are names of reservations.