Friday, 30 April 2021

Export DHCP Scope Properties with Powershell

Export DHCP Scope Properties with Powershell

Use this script to export DHCP scope properties (to a .csv file) with Windows Powershell

To view all available DHCP scope ID's, use the command (run it from the local DHCP server)

Get-DhcpServerv4Scope

Set the output file using the $outfile variable

$outfile = "C:\path\outputfile.csv"

You can then add the scopes you wish to include to the $dhcpscopeids list

$dhcpscopeids = "10.0.0.0", "10.1.0.0", "10.2.0.0" 

Cycle through each scope, retrieve the properties into the $scopeprops variable, then export the details to the $outfile CSV file. In this example, all DHCP scope properties are exported (appended) to a single file

foreach ($dhcpscope in $dhcpscopeids)
    {
    $scopeprops = Get-DhcpServerv4Scope -computername localhost -scopeid $dhcpscope
    $leases = Get-DhcpServerv4Lease -computername localhost -allleases -scopeid ($scopeprops.scopeid)
    $leases | export-csv "$outfile" -NoTypeInformation -append
    }