Jeff Wouters's Blog

Wake On Lan through PowerShell

by on Sep.05, 2013, under DuPSUG, PowerShell, Script

This morning I was asked to write a little script to do a WOL through PowerShell. Instead of writing it myself, I found this script.

As a scriptaholic I can’t just copy-paste and be done with it… I have to make it more suitable for my customer. 🙂
So, I’ve changed/added a few things.

  • A proper parameter
  • A little (crude) error handling
  • Allowing for multiple MAC addresses as input
  • Allowing for pipeline input
  • Just a few other things to make it more understandable for myself

Have fun!

function Send-WOL {
    param (
    foreach ($MAC in $MACAddress) {
        try {
            $MAC = $mac.split(':') | %{ [byte]('0x' + $_) }
            $UDPclient = new-Object System.Net.Sockets.UdpClient
            $Packet = [byte[]](,0xFF * 6)
            $Packet += $MAC * 16
            Write-Verbose ([bitconverter]::tostring($Packet))
            [void] $UDPclient.Send($Packet, $Packet.Length)
            Write-Output "WOL command sent to $MAC"
        } catch [system.exception] {
            Write-Output "ERROR: Unable to send WOL command to $MAC"

Leave a Reply