Wake on LAN all computers in a LAN retrieving the MAC addresses from a Windows DHCP Server

If you’re a network administrator, you may need to occasionally access and manage computers on your network remotely. This can be especially useful if you need to perform tasks or make changes to multiple computers at once. One way to accomplish this is to use PowerShell to get a list of all the computers on your network, and then use Wake-on-LAN (WoL) to remotely power on the computers that are currently powered off.

To get a list of all the computers on your network, you can use the PowerShell cmdlet Get-DHCPServerv4Lease, which retrieves all the DHCP leases on a DHCP server. This cmdlet returns a list of objects, each of which represents a DHCP lease on the server. The objects contain various properties, including the IP address, MAC address, and hostname of the leased computer.

To use the Get-DHCPServerv4Lease cmdlet, you’ll need to specify the DHCP server that you want to retrieve leases from. You can do this by using the -ComputerName parameter, followed by the name of the DHCP server. For example, the following command retrieves all the DHCP leases on a server named “DHCP1”:

Get-DHCPServerv4Lease -ComputerName DHCP1

Once you have a list of all the DHCP leases on your network, you can use the Send-WOL cmdlet to remotely power on any computers that are currently powered off. The Send-WOL cmdlet allows you to send a “magic packet” to a target computer’s network card, which will power on the computer if Wake-on-LAN is enabled.

To use the Send-WOL cmdlet, you’ll need to specify the MAC address of the target computer. You can use the -MACAddress parameter to do this. For example, the following command sends a magic packet to the computer with the MAC address 00-11-22-33-44-55:

Send-WOL -MACAddress 00-11-22-33-44-55

If you want to power on multiple computers at once, you can use the Send-WOL cmdlet in a loop, and specify the MAC address of each computer in turn. For example, the following code sends a magic packet to each computer in the list of DHCP leases that was retrieved earlier:

$computers = Get-DHCPServerv4Lease -ComputerName DHCP1

foreach ($computer in $computers) {
  Send-WOL -MACAddress $computer.MACAddress
}

Using PowerShell, it’s easy to get a list of all the computers on your network and remotely power them on using Wake-on-LAN. This can save you time and effort when you need to perform tasks or make changes to multiple computers at once.

My implementation i use to do maintenance work for many customers is by using this script and a Windows Scheduled Task to execute it every Saturday at 9:00 in the morning

function Wake-Computer {
    param (
        $MacAddress
    )
    $MacByteArray = $MacAddress -split "[:-]" | ForEach-Object { [Byte] "0x$_" }
    [Byte[]] $MagicPacket = (, 0xFF * 6) + ($MacByteArray * 16)
    $UdpClient = New-Object System.Net.Sockets.UdpClient
    $UdpClient.Connect(([System.Net.IPAddress]::Broadcast), 7)
    $UdpClient.Send($MagicPacket, $MagicPacket.Length)
    $UdpClient.Close()
}

$MACs = Get-DhcpServerv4Scope | Get-DhcpServerv4Lease

foreach ($m in $MACs) {
    Write-Host 'HostName: ' $m.HostName ' with MAC: ' $m.ClientId
    Wake-Computer -MacAddress $m.ClientId
}