PowerShell - Dhcp Across Networks

Written on June 30, 2019

With all the benefits that DHCP has to offer, it would be crazy if we needed to put a DHCP server in each network we setup. With this post, I will go through the steps of getting your Windows DHCP server working across whatever networks you want.


To follow along with this guide you should have a setup with at least the following:

  • Active Directory Forest
    • 1 Domain Controller
    • Guide
  • Dhcp server role on a server
    • Can be it’s own server, or on a DC
    • Guide
  • RRAS role installed on a server
    • Should be on it’s own server
    • Guide
  • 1 test server on new network

My setup

2 networks:

    • Has the Domain Controller (
    • Has the Dhcp server (
    • RRAS server:
      • IP address:
      • NetAdapter Name: Ethernet0
    • Has the test server
    • RRAS server:
      • IP address:
      • NetAdapter Name: Ethernet1

The Journey

The configuration can be broken down into 3 steps:

  1. Get information about the Dhcp server (IP address, DNS host name)
  2. Create Dhcp scope for the new network
  3. Install and configure the Dhcp relay agent

You can run all the code below directly from your RRAS server.

1. Get Dhcp server information

We need to know the hostname or IP address of the Dhcp server. We will also need to add a Dhcp scope for this new network ( in this example).

# IP address or DnsHostName of your Dhcp Server
$dhcpServer = 'dhcp.codeAndKeep.com'

# Name of the network adapter to enable DHCP on
$routerNetAdapterName = 'Ethernet1'

$dhcpAddress = [Net.Dns]::GetHostEntry($dhcpServer)
  Write-Warning "Unable to identify IP address of [$dhcpServer]"
  $dhcpServerIP = $dhcpAddress.AddressList[0]

2. Create new Dhcp scope

We will need to create a new Dhcp scope for our second network. Be sure to change the IP addresses to what you want/have configured.

# If you have access to the dhcp server 
## You can create your Dhcp scope remotely
$dhcpSession = New-PSSession -ComputerName $dhcpAddress.HostName
Import-PSSession -Session $dhcpSession -Module DhcpServer

# You can run the commands below directly on your DHCP Server
## If you don't want to use a PSSession

# Be sure to switch your Start and End Ranges if they are different
$newScope = Add-DhcpServerv4Scope -Name RemoteNet0 `
  -StartRange `
  -EndRange `
  -SubnetMask `
  -State InActive `

Set-DhcpServerv4OptionValue -ScopeID $newScope.ScopeId `
  -DnsDomain codeAndKeep.com `
  -DnsServer `

Set-DhcpServerv4Scope -ScopeId $newScope.ScopeID -State Active

# Not necessary if you ran this on directly on your Dhcp server
Remove-PSSession $dhcpSession

3. Install a Dhcp relay agent

This was the most difficult thing to figure out, as there is no PowerShell cmdlets to configure this. Having worked with netsh previously, I was able to find some documentation around configuring a relay agent.

In the code below, we will create a netsh script file to execute. This script will be populated with the IP address of the Dhcp server, as well as the interface alias that the relay agent will use.

pushd routing ip relay
set global loglevel=ERROR
add dhcpserver $($dhcpServerIP.IPAddressToString)
add interface name="$routerNetAdapterName"
set interface name="$routerNetAdapterName" relaymode=enable maxhop=6 minsecs=6


# Create netsh script file
New-Item -Path $netshDhcpRelayPath `
  -Type File `
  -ErrorAction SilentlyContinue | Out-Null

# Populate contents of the script 
Set-Content -Path $netshDhcpRelayPath `
  -Value $netshDhcpRelay.Split("`r`n") `
  -Encoding ASCII

# run it
netsh -f $netshDhcpRelayPath

Test Dhcp on new network

Finally to ensure it is configured and working, we need a computer on the new network. If there is one already running you can restart the network adapter to start the Dhcp client. If it was configured with a static IP address, you should remove it.

Get-NetAdapter | Restart-NetAdapter

On your RRAS server, you can check the statistics of your relay agent. You can use this for troubleshooting or just to see some statistics.

netsh routing ip relay show ifstats

You should see something like this: _config.yml

You can check your Dhcp server as well to see if a lease has been issued.

Get-DhcpServerv4ScopeStatistics -ScopeID $newScope.ScopeId

Thanks for reading

PS> exit