Pull DHCP info using PowerShell

This short script will get all the DHCP servers authorized in your Windows domain, and pull all the scopes and IPs. It exports these to two separate CSV files.

$DHCPServers = Get-DhcpServerInDC
If (Test-Path -Path $env:TEMP\Scopes.csv) {Remove-Item $env:TEMP\Scopes.csv}
If (Test-Path -Path $env:TEMP\Leases.csv) {Remove-Item $env:TEMP\Leases.csv}
If (Test-Path -Path $env:TEMP\Leases.csv) {Remove-Item $env:TEMP\DNSExport.csv}
foreach ($_ In $DHCPServers) {
    Get-DhcpServerv4Scope -ComputerName $_.DnsName | Select-Object -Property ScopeId,SubnetMask,Name,State,StartRange,EndRange,LeaseDuration,Description,type | Export-Csv $env:TEMP\Scopes.csv -Append -NoTypeInformation
    Get-DhcpServerv4Scope -ComputerName $_.DnsName | Get-DhcpServerv4Lease -ComputerName $_.DnsName -AllLeases | Select-Object -Property IPAddress,ScopeId,ClientId,HostName,AddressState,LeaseExpiryTime,ClientType,Description,DnsRegistration,DnsRR,ServerIP    | Export-Csv $env:TEMP\Leases.csv -Append -NoTypeInformation
    }

The formatting for the script is a little off.  I need to get a code display plugin, but I am too lazy.

Leave a Reply