Get Email Alerts for DHCP Scope Full Warnings in Windows Server 2012/2016

Windows DHCP servers used to have email alert functionality built-into the task creation process when events are triggered. Here are steps to use a PowerShell script to replace this functionality and how to customize it. This was split on several different posts, so consolidating it here.

1. PowerShell script – From Cazi Brasga

#DHCP Percent of Addresses Remaining Threshold
$DHCPLowPercentThreshold = 10
#From Email Address
$FromAddress = "dhcpalert@domain.com"
#To Email Address
$ToAddress = "user@domain.com"
#SMTP Server Address
$SmtpServerAddress = "mail.domain.com"

$5MinutesAgo = [DateTime]::Now.AddMinutes(-5)
$DHCPLowEvent = @(Get-EventLog -LogName "System" -After $5MinutesAgo -InstanceID 1020)[0]
$DHCPLowEventMessage = $DHCPLowEvent.Message
$DHCPLowPercent = 100 - $DHCPLowEvent.ReplacementStrings[1]
If ($DHCPLowPercent -le $DHCPLowPercentThreshold){
$messageParameters = @{ 
Subject = "Warning: DHCP Addresses Low" 
Body = "$DHCPLowEventMessage"
From = $FromAddress
To = $ToAddress
SmtpServer = $SmtpServerAddress
} 
Send-MailMessage @messageParameters
}

2. Run your task from “System” user account, so that you do not have to be logged on with your account to run the task.

3. Finally, adjust the threshold of alerting you want.

Open Regedit.exe
Navigate to this path
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dhcpserver\Parameters\
Create a REG_DWORD (32 bit) value named DhcpAlertPercentage under Parameters
set it to 85 or 90 (decimal)
Lastly, restart the DHCP service for the change to become active.