Wednesday, August 14, 2013

Using Netsh to export and import DHCP reservations

Recently I had to move 80 or so virtual servers from one subnet to another.  The server's all have their IP's assigned via DHCP, and they keep them via DHCP reservations.  I also need to make the IP's reserved in the new scope.

This should be easy peasey.

I just need to use netsh to export the DHCP scope in question, do a find and replace to change the address, and re-import.

First, let's review the syntax for using the netsh export command.

  1. First you launch a netsh command prompt C:\Users\Administrator>netsh.exe
  2. Now that you have a Netsh command prompt, navigate to the DHCP commands.
    dhcp server \\servername
  3. Now issue the export command. The following command exports the full service configuration to the c:\Temp\Dhcpdb file:
    export c:\temp\dhcpdb all
    The following command exports the configuration that pertains to scopes and to the c:\Temp\Dhcpdb file:
    export c:\temp\dhcpdb

Wait a minute, there's a note on the netsh.exe dhcp command documentation:

This command works only on the local server.
While the export command runs, the DHCP service is stopped and does not respond to DHCP clients seeking new leases or lease renewals.
If the DHCP service has a large number of scopes or a large number of client address leases, this command can take a long time to run.

Well, doesn't that just suck that the DHCP service has to stop.  Not quite as easy peasey as I hoped.

That leads us to the DUMP command:

Dumps the configuration of the local DHCP server to the command prompt window when run within the netsh environment.

So using the DUMP command, you dump the entire config, and just copy the bits that you need, and reimport them.
So running the command (from an administrative command prompt):
C:\Users\Administrator>netsh dhcp server dump > dhcpcfg.txt
generates a file in that directory from the local dhcp server.

  • Note, you should be able to run the command remotely, but apparently there is/was a bug that prevented windows 7 and vista from accessing that command.  Works fine on XP and 2000 however. Go figure

Open up the file, and find the ReservedIP section for the scope you are interested in. It's very human readable, you shouldn't have any problem finding it.
The section of the file I wanted appears like this (of course I sanitized it):

# ======================================================================
#  Start Add ReservedIp to the Scope :, Server :            
# ======================================================================

Dhcp Server Scope Add reservedip 001122334455 "Server1" "" "DHCP" 
Dhcp Server \\DHCPSrv1 Scope Add reservedip 001122334456 "Server2" "Web Server" "BOTH"
Dhcp Server \\ Scope Add reservedip 001122334457 "Server3" "ISA Server" "BOTH"

So I copy JUST this section to another blank text file so I can edit.

Let's cover the syntax:
The stuff surrounded in == and # are just comments, they're there to help a human read the file. Nothing more.
Dhcp Server
Dhcp Server \\DHCPSrv1
Dhcp Server \\
These are connection methods to identify the DHCP server, The first via IP, the second via NetBIOS, the third via FQDN.  Normally they would all be the same, I just did three to show the three different ways.
Scope Add reservedip
Identifies the scope you want to edit, and what you want to do. 001122334456 "Server2" "Web Server"

The IP address you want to reserve, the MAC address it's bound to, The Reservation Name, the Reservation Description (Which is optional).


Specifies the type of clients to associate with this reserved client entry. The options are {DHCP | BOOTP | BOTH}  DHCP is the default.

Now that you have your file all fixed up, you need to import it back in.
You have a few options.

  1. The easiest option is to launch the netsh.exe command from a command window, and just paste the file into the command window. This works best for small changes.
  2. Import the configuration changes using the netsh import command.  Crap, the import command stop's the DHCP server as well.
  3. Import the configuration changes using the netsh exec command.

I'm not going to cover the netsh import command, because it's going to stop the DHCP server.  Not good, I want to do this live, with no impact on my normal clients.

Use netsh exec to import the configuration changes
C:\Users\Administrator>netsh exec dhcpcfg-edited.txt


  1. Thanks for this post, together with this was really helping us to import our 20+ dhcp reservations from SBS 2003 to SBS 2011.

  2. Excellent post, thanks very much. Saved me a lot of time

  3. Thanks a lot for this mate, great job!

  4. Great Information, but in the Agency I work, We the Hetwork Engineers are responcible for IPAM, we manage the DHCP servers via MMC access normally. The export and import commands only work when using netsh on the local host; when attempting to run the export from another server or my desktop I would get the following error:
    netsh dhcp server scope>export c:\temp\MyFile

    DHCP Server import and export are allowed on local servers only.
    Error 87 in FormatMessageW()

    Parameter(s) passed are either incomplete or invalid.
    netsh dhcp server scope>

    But you gave me enough to find the show reservedip command and the add reservedip commends to make it work.

    Just have to copy paste from the shell to text edit it and paste it back. Was only 100 reservations in a single scope, this time!

    1. There is a note about that about that about halfway thru my Blog post

  5. Use this. from another site to automate it in a for loop

    for /f "tokens=1-6 delims=," %G IN (lists.txt) do netsh dhcp server \\server scope %G add reservedip %H %I %J %K %L

  6. Thank you very much, for this guide! This helped me out immensely when I had to migrate DHCP scopes from a Server 2008 R2 server to a Server 2008 DHCP server!

  7. Wonderful post! Saved me from recreating 65 scopes and hundreds of reservations!!! Thank you very much.