Home > DHCP, Powershell Scripts > Export DHCP leases to HTML– version 2

Export DHCP leases to HTML– version 2

An updated version of the previous script, now with the ability to export the MAC to the HTML as well as dumping all scopes to individual HTML and .csv files


$sort = "DNS" #output file sorting, can also be "DNS" or "MAC" (MAC only if includeMAC switch selected

Function DumpDHCPScope (){

$scopedump = (netsh dhcp server $DHCPServer scope $subScope show clients 1)
$lines = @()
foreach ($i in $scopedump){
	if ($i -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"){
		If ($i -match "[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}"){	
			$lines += $i.Trim()
$csvfile = @()
foreach ($l in $lines){
	$Row = "" | select IP, DNS
	If ($IncludeMAC -eq $true){$Row = "" | select IP, DNS, MAC}
	#$l = $l -replace '[0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}[:-][0-9a-f]{2}', '' #removes MAC address
	$l = $l -replace ' - ',','
	$l = $l -replace '\s{4,}',''
	$l = $l -replace '--','-'
	$l = $l -replace '-D-','-'
	$l = $l -replace '[-]{1}\d{1,2}[/-]\d{1,2}[/-]\d{4}',''
	$l = $l -replace '\d{1,2}[:]\d{2}[:]\d{2}',''
	$l = $l -replace 'AM',''
	$l = $l -replace 'PM',''
	$l = $l -replace '[,][-]',''
	$l = $l -replace '\s{1}',','
	$l = $l -replace '[,]{2,4}',','
	$l = $l -replace '[,][-]',''
	#$l = $l + "`n"
	$Row.IP = ($l.Split(","))[0]
	#$Row.SubNetMask = ($l.Split(","))[1]
	if ($IncludeMAC -eq $true){$Row.MAC = ($l.Split(","))[2]}
	$Row.DNS = ($l.Split(","))[3]
	$csvfile += $Row

$csvfile | sort-object $sort | Export-Csv "C:\TMP\ScriptOut\$subScope-DHCPOut-List.csv" -NoTypeInformation

$a = "<style>"
$a = $a + "body {margin: 10px; width: 600px; font-family:arial; font-size: 12px;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: rgb(179,179,179);align='left';}"
$a = $a + "TD{border-width: 1px;padding: 2px;border-style: solid;border-color: black;background-color: white;}"
$a = $a + "</style>"

"Please contact theadminguy@wordpress.com in case of any questions" | Out-File "C:\TMP\ScriptOut\$subScope-DHCP-List.html"
$csvfile | sort-object $sort | ConvertTo-HTML -head $a | Out-File -Append "C:\TMP\ScriptOut\$subScope-DHCP-List.html"
get-date -Format dd/MM/yyyy@HH:mm:ss  | Out-File -Append "C:\TMP\ScriptOut\$subScope-DHCP-List.html"

}#End Function DumpDHCPScope

#Main Script

if ($Scope -eq "ALL"){
	$scopelist = @()
	$AllScopes = (netsh dhcp server $DHCPserver show scope 1)
	foreach ($i in $AllScopes){
		if ($i -match "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"){
			$scopelist += $i.Trim()
	Foreach ($i in $scopelist){
		$i = $i -replace '\s{2,}',''
		$i = $i -replace '- ',','
		$i = $i -replace '-',','
		DumpDHCPScope $i.Split(",")[0]
Else {
	DumpDHCPScope $scope

Example commandlines:

Export-dhcptohtml.ps1 –dhcpserver –scope –includemac

This will dump the DHCP scope including the MAC to an HTML file as well as a csv file.

Export-dhcptohtml.ps1 –dhcpserver -scope ALL

This will dump all scopes on the dhcp server to individual HTML and .csv files.

Do post a comment, suggestion or request.

[Update 29-05-2012] – Added missing – (dash) in second example


Categories: DHCP, Powershell Scripts
  1. RH
    13/04/2012 at 22:09

    would it be possible to include the lease-expiry date too in the output ?

  2. wayne
    11/05/2012 at 20:55

    Second example above shouldn’t that be “-scope” not “scope”

    • The Admin Guy
      28/05/2012 at 22:51

      That is correct – Updated

  3. 05/07/2012 at 22:50

    Is there a easy way to leave the lease-expiry date in the output as well.

  4. sharkbelly
    05/07/2012 at 22:51

    I will ask again since it has not been responded to. Is there a way to have the lease-expiry date in the output as well. Or at least like the MAC option have it in there as well. Would be greatly appreciated.

  5. Brendan
    03/08/2012 at 14:47

    Hello and thank you so much for offering this. Couple of questions/requests:
    1.) The script works however there are no DNS machine hostnames recorded in the output even though I have them present. I see the IP address and MAC address but never any hostnames. Do you have any thoughts?
    2.) Can the MAC address be formatted like xx:xx:xx and not xx-xx-xx?
    3.) Can all of the scope output be merged into a single file master .csv file?

    Basically we are tying to take one output with the IP, Hostname and MAC address of all DHCP hosts so we can import it into a security appliance.

    Thank you!

  6. Priester
    15/08/2012 at 08:24

    Hi i struggle to get anything out of the script it returns blank info when it creates the files

  7. Ezequiel
    07/02/2013 at 14:42

    I´m receiving blanck csv file as output. Can you help me with this?

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: