Home > PowerShell Scripting > Simple Wake On Lan powershell Function.

Simple Wake On Lan powershell Function.

I recently wrote the below powershell module for waking up my home lab. There’s a great article on The Powershell Guy to do with Wake On Lan, but it didn’t include a function or any specific error checking.

Below is the module I put together quickly for the purpose.

function send-wakeonlan{
    param(
        [string]$mac)
    if (!($mac -like "*:*:*:*:*") -or ($mac -like "*-*-*-*-*")){
    write-error "mac address not in correct format"
    break
    }

    $string=@($mac.split(":""-") | foreach {$_.insert(0,"0x")})
    $target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
}

The usage of this function is below:

send-wakeonlan -mac “3C:4A:92:77:AA:21”

send-wakeonlan -mac “3C-4A-92-77-AA-21”

  1. Mark Nelson
    August 19, 2012 at 12:46 pm

    This is exactly what I was looking for… Thanks a lot for sharing.

  2. Hugh Poling
    September 2, 2012 at 4:31 pm

    Your if statement wasn’t working. I specified dashes in the mac the if statement would error out. The corrected version of the if statement:

    if (-not (($mac -like “*:*:*:*:*”) -or ($mac -like “*-*-*-*-*”))){
    write-error “mac address not in correct format”
    break
    }

    Also, I didn’t need to use the “-mac” switch in the syntax.

    Both of these worked for me after changing the if statement as above:

    send-wakeonlan “3C:4A:92:77:AA:21″
    send-wakeonlan “3C-4A-92-77-AA-21″

    • September 2, 2012 at 5:49 pm

      Thanks for sharing! The Mac parameter is positional, you don’t need to specify it as its assumed.

      I’ll update the code to include your fix for the dashes.

  3. George
    November 14, 2012 at 11:28 am

    Hey buddy,
    great post, just a quick question from a powershell starter:
    6..101 |% { $packet[$_] = $target[($_%6)]}
    What does this part do?

  4. Gustav Krantz
    December 20, 2012 at 7:21 am

    <- Also as powershell starter. If i would want to
    run it as: send-wakeonlan i.e. send-wakeonlan tv Instead of having
    to put in the mac for every computer i want to start. Can you
    rewrite the script to use like… if ((tv = string) send to ?
    Thanks!

    • December 20, 2012 at 12:46 pm

      You will need to specify the MAC address of the device, its unavoidable.

      • Gustav Krantz
        December 20, 2012 at 1:00 pm

        It should’nt be if it predefined in the script. 🙂

  5. Gustav Krantz
    December 21, 2012 at 2:05 pm

    Sure it’s possible. I just did it…

    $tv = “6C:62:6D:87:DB:A5”
    $seeker = “0C:0C:0C:0C:01”
    $server = “00:22:15:88:AE:29”
    $virwin = “F4:6D:04:34:06:91”
    function wol{
    param(
    [string]$mac)
    if (($mac -eq “tv”)){
    $tv=@($tv.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($tv[0], $tv[1], $tv[2], $tv[3], $tv[4], $tv[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    if (($mac -eq “seeker”)){
    $seeker=@($seeker.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($seeker[0], $seeker[1], $seeker[2], $seeker[3], $seeker[4], $seeker[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    if (($mac -eq “server”)){
    $server=@($server.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($server[0], $server[1], $server[2], $server[3], $server[4], $server[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    if (($mac -eq “virwin”)){
    $virwin=@($virwin.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($virwin[0], $virwin[1], $virwin[2], $virwin[3], $virwin[4], $virwin[5])

    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    $UDPclient.Send($packet, $packet.Length) | out-null
    }
    }

    All I need to do now is run:
    wol tv/seeker/server/virwin and the script will wake the computer i tell it to.

    No problem.

  6. Gustav Krantz
    December 21, 2012 at 5:07 pm

    I’m as you know a noob at that. Would you please give me an example of the code for this script?

  7. Gustav Krantz
    December 21, 2012 at 5:28 pm

    That switch statement didnt help me much. I dont know how that will help me.

  8. January 4, 2013 at 8:39 am

    Hi Gustav,

    here is my recommendation:

    http://pastebin.com/SYthJH4f

  9. Markus Ressel
    February 17, 2013 at 9:47 pm

    Hey there,

    your code still is not updated. The test for wrong input is simply useless since you can enter anything after and before the last/first dash/colon.

    Anyway thanks a lot for sharing this, made my life a lot easier!
    Keep up the good work.

  10. Markus Lohr
    July 23, 2013 at 3:00 pm

    Hey guys,
    here some improvements (on my side of the world the mac-address has 6 segments… ) 😉
    ——————————————————

    function send-wakeonlan{
    param(
    [string]$mac)
    # error checking
    if (-not(($mac -like “*:*:*:*:*:*”) -or ($mac -like “*-*-*-*-*-*”))){
    write-error “mac address not in correct format”
    break
    }
    #rewrite the mac-format
    $string = @($mac.split(“:””-“) | foreach {$_.insert(0,”0x”)})
    $target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5])

    #building the magic package
    $UDPclient = new-Object System.Net.Sockets.UdpClient
    $UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
    $packet = [byte[]](,0xFF * 102)
    6..101 |% { $packet[$_] = $target[($_%6)]}
    #sending the magic package
    $UDPclient.Send($packet, $packet.Length) | out-null
    }

    #PS stuff
    Export-ModuleMember -Function send-wakeonlan
    ——————————————————
    have fun….

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: