DHCP PowerShell in Windows Server 2012, in addition to providing cmdlets for DHCP server management, provides cmdlets for exporting (Export-DhcpServer) and importing (Import-DhcpServer) DHCP Server configuration and IP address lease data. Unlike NetSH.exe export/import functionality which uses binary file format for the exported data, these cmdlets are designed for human readability as well as modification of the exported data and hence provide the exported data in an XML file.
Powershell import and export functionality provides the ability to migrate DHCP server configuration (optionally including lease data) from source server to a destination server. Here are some key administrative scenarios which are addressed by the export/import functionality:
DHCP Powershell Export/Import works on published XML schema which is part of the DHCP Powershell module installation and can be found at $PSHOME\Modules\DhcpServer location.
Export-DhcpServer [-File] <String> [-ScopeId <IPAddress>] [-Prefix <IPAddress>] [-Force] [-Leases] [-ComputerName <String>] [-CimSession <CimSession>] [<CommonParameters>]
The Export-DhcpServer cmlet exports DHCP server and lease configuration information to an XML file specified by ‘File’ parameter. The scopeId (IPv4) and prefix (IPv6) parameters allow the admin to export only the specified IPv4 or IPv6 scopes respectively and thus provide a mechanism to selectively migrate specific scopes. Along with the specified scopes, the export cmdlet will also export server level configuration i.e. server level option values, option and class definitions, MAC address filters. This ensures that the effective configuration of the specified scopes on the destination server when this file is imported is same as the destination server.
If neither ScopeId nor Prefix is specified, the entire server configuration including all scopes (v4 and v6) is exported by the cmdlet. If Leases switch is specified, the lease data is also exported in addition to configuration data. Since the lease data can get voluminous, running Export-DhcpServer with –Leases is expected to take considerably longer time than exporting just configuration data.
If ComputerName or CimSession parameter is given, configuration from specified remote DHCP server is exported. If XML file to be exported already exists, cmdlet will throw an error. You can use the Force parameter to overwrite already existing XML file.
Export is supported from Windows Server 2008, Windows Server 2008 R2 and Windows Server 2012.
Import-DhcpServer [-File] <string> [-BackupPath] <string> [-ScopeId <ipaddress>] [-Prefix <ipaddress>] [-ScopeOverwrite] [-Leases] [-ServerConfigOnly] [-Force] [-ComputerName <string>] [-CimSession <CimSession>] [-WhatIf] [-Confirm] [<CommonParameters>]
The Import-DhcpServer cmdlet imports the DHCP server configuration (and optionally lease data) from the file specified by ‘File’ parameter. Just as in the case of Export-DhcpServer, using ScopeId (IPv4) and/or Prefix (IPv6) parameters, the admin can selectively import the specified scopes/prefixes. As noted under Export-DhcpServer, the server level configuration is imported as well when scopes/prefixes are specified for reasons mentioned earlier.
If neither ScopeId nor Prefix is specified, all the configuration contained in the file (and optionally lease data) will be imported by the cmdlet. If Leases switch is specified, the lease data in specified file is also imported in addition to configuration data. Import operation with leases is expected to take longer depending on the number of leases being imported.
If ServerConfigOnly switch is specified, only server level configuration will be imported on the destination server. If ComputerName or CimSession parameter is given, configuration from specified remote DHCP server is exported.
Before starting the actual import operation, the cmdlet does a backup of the DHCP Server to the path as specified by the ‘BackupPath’ parameter. This gives administrator a way to restore to the original settings in case there is a terminating error during the import operation.
The –ScopeOverWrite allows the admin to continue with import even if the scope is already present on the destination DHCP server. If this parameter is not specified, the import cmdlet gives a non-terminating error for a scope which is already present on the DHCP server and continues to process the next scope in the file.
The cmdlet seeks a user confirmation which can be overridden using the –Force parameter.
Import is supported on DHCP server running on Windows Server 2012.
Here is the comparison table comparing the PowerShell Export/Import cmdlets and the older netsh export/import -
Uses an XML file format for export/import
Uses a binary file format for export/import
Easy to modify/update or even create a new XML file for import/export since XML schema is published.
Not possible to read, update or create the binary file used for import/export.
Can export from or import to a remote DHCP Server
Works only on local DHCP Server
The DHCP Server service can continue uninterrupted during exporting or importing DHCP Server
DHCP Server service is stopped while exporting from or importing to the DHCP server
Gives flexibility of opting if lease data should be exported/imported
Lease data is always exported/imported.
Time taken by PowerShell Export/Import is longer than NetSH Export/Import as PowerShell export/import uses the RPC transport to read/write data to the DHCP server.
Below are some examples to elucidate the usage of Powershell Export, Import cmdlets:
1. Export-DhcpServer -File C:\dhcpexport.xml -ComputerName dhcpserver.contoso.comThe above command will export all the DHCP server (DHCPv4 and DHCPv6 configuration including all the IPv4 and IPv6 scopes present on the ‘dhcpserver.contoso.com’ server to ‘dhcpexport.xml’ file
2. Export-DhcpServer -File C:\exportdir\dhcpexport.xml -ScopeId 10.10.10.0,220.127.116.11 -ComputerName dhcpserver.contoso.comThe above command will export scopes ‘10.10.10.0’ and ‘18.104.22.168’ present on the ‘dhcpserver.contoso.com’ server to the export file ‘dhcpexport.xml’. The DHCPv4 server level configuration will also be exported to the specified file.
3. Export-DhcpServer .\dhcpexport.xml –Prefix 3ffe::1 -Leases
The above command will export the IPv6 prefix ‘3ffe::1’ from the local DHCP Server to the XML file ‘dhcpexport.xml’ in the directory from where the cmdlet is run including all the leases in the IPv6 scope. The DHCPv4 server level configuration will also be exported to the specified file. Note that since File parameter is positional, the parameter name is not explicitly given.4. Import-Csv ScopeList.txt | Export-DhcpServer -File C:\exportdir\dhcpexport.xml -Leases The above command exports a list of scopes specified in ScopeList.txt to the specified export file in XML format. The above command works by pipelining the objects containing the ScopeIds outputted by Import-Csv to the second cmdlet Export-DhcpServer which exports the specified scopes. The ScopeList.txt file should contain the following format: ScopeId 10.10.10.0 22.214.171.124 126.96.36.199
5. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:\exports\dhcpexport.xml -BackupPath C:\dhcpbackupThe above command imports the configuration data from file ‘dhcpexport.xml’ onto the server dhcpserver.contoso.com. All the configuration data contained in the xml file other than leases is imported. If an IPv4 or IPv6 scope is present in both the XML file and the target machine ‘dhcpserver.contoso.com’, such scopes are not migrated and a warning for the same notifying that the scope is not migrated is shown.6. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:\exports\dhcpexport.xml -BackupPath C:\dhcpbackup\ -leases The above command imports the configuration data from file ‘dhcpexport.xml’ onto the server dhcpserver.contoso.com. All the configuration data contained in the xml file included lease data are imported.7. Import-DhcpServer C:\exports\dhcpexport.xml C:\dhcpbackup -ScopeId 10.10.10.0 –Prefix 3ffe::1 –Leases -Force
The above command imports the configuration settings including lease data for scopes 10.10.10.0 and 3ffe::1 from the XML file ‘dhcpexport.xml’ onto the local DHCP server. If the export file contains other scopes besides 10.10.10.0 and 3ffe::1, the same are ignored. Server level configuration data for both IPv4 and IPv6 if present in the export file is also imported onto the server. Import cmdlet by default seeks for user confirmation. The ‘-Force’ parameter suppresses the default confirmation prompt.8. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:\exports\dhcpexport.xml -BackupPath C:\dhcpbackup\ -Leases -ScopeOverwrite
The above command imports the configuration and lease data for all the IPv4 and IPv6 scopes from the XML file ‘dhcpexport.xml’ onto the server dhcpserver.contoso.com. DHCPv4 server level configuration data if present in the export file is also imported onto the server. If the scopes present in the xml file are already present on the server dhcpserver.contoso.com, those are deleted and recreated from the data in the export file.9. Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:\exports\dhcpexport.xml -BackupPath C:\dhcpbackup\ -ServerConfigOnly The above command imports only the server level configuration data in the specified file onto the server dhcpserver.contoso.com. Any scope configuration data if present in the file is ignored.10.
$ScopeIdList = Get-Content ScopeList.txt Import-DhcpServer -ComputerName dhcpserver.contoso.com -File C:\exportdir\dhcpexport.xml -Leases -ScopeId $ScopeIdList The above set of commands imports a list of scopes specified in ScopeList.txt to the server dhcpserver.contoso.com. The first command Get-Content gets the list of scopes to be imported in the variable ScopeIdList. The same is then used as input parameter ScopeId of the second command to import the scopes. The ScopeList.txt file should contain the following format:
10.10.10.0 188.8.131.52 184.108.40.206
Hope these new set of PowerShell tools are useful to the Windows DHCP administrator!
As always, we are keen to hear your feedback!
I am not agree on point NETSH Export is not human readable.
Netsh DHCP export can be set to .txt file& is editable and used for import again.
Also NETSH DHCP backup works for remote servers. Online backup is also possible by NETSH for DHCP.
Yhea, so we are doing a change to some ranges and whilst doing so I discovered that in 2012R2 one cannot simply define the -BackupFilePath. Instead, I was able to use -BackupPath FTW. This post is otherwise spot-on for 2012R2.
I forgot to mention one other thing, in relation to Shrikrishna's post. I was able to easily edit the .xml file with notepad and it imports fine. Careful you don't change the character encoding or anything. I'd advise making the copy of the file, renaming
it, and then editing that copy rather than saving-as.
Thanks for pointing out, Bubbafet. I have corrected the examples to -BackupPath.