Press "Enter" to skip to content

PowerShell – Find computer details in DHCP scopes based of MAC address

Reading Time: < 1 minute

Quick and dirty PowerShell script to search DHCP servers and scopes for computer details like: IP address, hostname, DHCP address state and DHCP scope computer is is. It uses MAC address of the network card to do the search. Script will return nothing if MAC address is not found in DHCP.

You need admin permissions on the DHCP server in order to run this, otherwise it will throw an exception.

$MACtoFind = "<enter mac here>"
$DHCPServers = Get-DHCPServerInDC | 
                Where-Object {($_.DnsName -like '*uk*') -and ($_.DnsName -like '*dhcp*')} | 
                select IPAddress, DNSName 

ForEach ($DHCPServer in $DHCPServers) 
{
    try 
    {
        Get-DhcpServerv4Scope -ComputerName $DHCPServer.DNSName | 
         select ScopeID |
         ForEach-Object {Get-DHCPServerv4Lease -ScopeId $_.ScopeId -ComputerName $DHCPServer.DNSName -AllLeases  | 
         where {$_.ClientID -eq $MACtoFind } | Select-Object ScopeId,IPAddress,HostName,ClientID,AddressState }

    }
    catch 
    { 
        write-host "Caught an exception:" -ForegroundColor Red
        write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
        write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Re
        write-host "Exception reason: $($_.Exception.NativeErrorCode)" -ForegroundColor Red
    }
}
Share & contribute
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.