Home > DevOps > Transfer DHCP Server IPv4 Scopes in Windows Server DHCP to another server with PowerShell

Transfer DHCP Server IPv4 Scopes in Windows Server DHCP to another server with PowerShell

I had a call the other day from a customer that wanted to move all of their IPv4 DHCP scopes in Microsoft DHCP Server to another server instance. They wanted to run the commands on the source server and then logon to the destination server and run the commands there as well.

To do this I wrote a quick PowerShell script to export all of the scopes and their configuration to an xml file.

To do this, I created a folder in the root of the C drive called dhcptemp where I could export all of the IPv4 DHCP scopes, their configuration, and leases to.

New-Item -Path C:\dhcptemp -Force

After that, it was the export of the IPv4 DHCP scopes.

Get-DhcpServerv4Scope | ForEach-Object {
    $ScopeID = $_.ScopeId
    Export-DhcpServer -File "C:\dhcptemp\Scope-$scopeId.xml" -ScopeId $ScopeID
}

That dhcptemp folder was then copied to root of the C drive on the destination server. To Import the DHCP Scopes, The following code was run on the new server.

$item = Get-ChildItem -Path C:\dhcptemp | Where-Object { $_.Name -like "*.xml" }

foreach ($i in $Item) {
    Import-DhcpServer -File $i.FullName -BackupPath C:\Windows\System32\dhcp\backup -ScopeOverwrite
}

After that, we just had to remove the dhcptemp folder.

Remove-Item C:\dhcptemp -Force -Recurse -Confirm:$false
Summary
Article Name
Transfer DHCP Server IPv4 Scopes in Windows Server DHCP to another server with PowerShell
Author