Manage DHCP Filters via PowerShell Script

6 July 2017 , ,

If you operate a list of allowed mac addresses on your DHCP server to prevent unauthorised users/devices from obtaining an IP address, it can be a pain to have to constantly open the DHCP console and disable/enable the list and/or add a new device to the list.

To make this easier, you can setup a quick PowerShell script which will manage this for you.

How this DHCP management PowerShell script will help you

When you run the script, it shows you the current status of your filters.

True means that the filter is enabled (only allows devices in the list to obtain IP addresses) and false means that it’s disabled (allow any device to get an IP address).

Then it asks you to select an option from the below:

  • [E] enable – this turns on the filter list and prevents devices that aren’t in the list from getting a dynamic IP address.
  • [D] disable – this turns off the list and allows any device to get a dynamic ip address.
  • [A] add – this allows you to add a new device to the list (requires mac address and device name).

I hope you’ll agree, that this makes DHCP management a lot more efficient and quicker.

Creating a PowerShell script to manage your DHCP filters

Please note that you need administrative rights over the server to manage your DHCP filter lists. With the recent increase in malware attachs, I’d recommend that your account isn’t a domain admin, so I’d recommend running this as another option.  I will produce another article explaining how to do this soon.

To make your own, follow the below easy steps.

  1. Start by creating an empty script file called ‘dhcp_filters.ps1’ and save it somewhere (eg on your desktop).
  2. Open ‘dhcp_filters.ps1’ in a test editor, I like Notepad++.
  3. Copy the below script.
  4. $servername = "ENTER_SERVER_NAME_HERE"
    function Add-MacAddress {
    Param ([string]$macaddress=(Read-Host "Enter a MAC address without hyphens and spaces"), [string]$machinename=(Read-Host "Enter a device name"))
    Add-DhcpServerv4Filter -ComputerName "$servername" -List Allow -MacAddress $macaddress -Description "$machinename"
    Get-DhcpServerv4FilterList -ComputerName $servername
    $title = "Enable/Disable DHCP Filters"
    $message = "Do you want to change the status of the DHCP filters?"
    $enable = New-Object System.Management.Automation.Host.ChoiceDescription "&enable", `
        "Enables DHCP Filters."
    $disable = New-Object System.Management.Automation.Host.ChoiceDescription "&disable", `
        "Disables DHCP Filters."
    $add = New-Object System.Management.Automation.Host.ChoiceDescription "&add", `
        "Add MAC address to DHCP Filters."
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($enable, $disable, $add)
    $result = $host.ui.PromptForChoice($title, $message, $options, 0) 
    switch ($result)
            0 {Set-DhcpServerv4FilterList -ComputerName "$servername" -Allow $True -Deny $True; Write-Host "DHCP filters are now enabled."}
            1 {Set-DhcpServerv4FilterList -ComputerName "$servername" -Allow $False -Deny $True; Write-Host "DHCP filters are now disabled."}
            2 {Add-MacAddress; Write-Host "MAC address added to DHCP filters."}
    Start-Sleep -s 5
  5. Replace ‘ENTER_SERVER_NAME_HERE’ with the name of your DHCP server.
  6. Save your script.
  7. Now just run the scripts and you can manage your DHCP filter lists more efficiently.

If you have any suggestions on how to improve on this script, please share them in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.