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.