Q. How can I easily send a magic packet to wake a machine on my subnet?

John Savill

January 10, 2011

2 Min Read
ITPro Today logo in a gray background | ITPro Today

A. In a previous FAQ, I talked about the Wake On LAN (WOL) magic packet, which wakes a machine that's turned off by sending a packet with the machine's MAC address. The target machine must support WOL, and you may need to configure whether the machine should boot normally or from the network when it's started via WOL.

I wanted a little script to send the magic packet to turn on my wife's desktop machine upstairs when she wants to use it. (She's on bed rest, pregnant with twins, and I got tired of going up there to turn it on ). She uses a small laptop and remotes into her desktop computer, so shutting down the desktop was just a matter of  creating a shortcut to

shutdown /s /t 0

Starting it was trickier, however. After checking that the BIOS supported WOL, I made a note of the MAC address and created the PowerShell script below based on some snippets I found on the web. You just pass the MAC address in format xx:xx:xx:xx:xx:xx and it turns on the machine. Easy!

Below is my PowerShell script:


param (\[String\]$MACAddrString = $(throw 'No MAC addressed passed, please pass as xx:xx:xx:xx:xx:xx'))
 $MACAddr = $macAddrString.split(':') | %\{ \[byte\]('0x' + $_) \}
 if ($MACAddr.Length -ne 6)
 \{
     throw 'MAC address must be format xx:xx:xx:xx:xx:xx'
 \}
 $UDPclient = new-Object System.Net.Sockets.UdpClient
 $UDPclient.Connect((\[System.Net.IPAddress\]::Broadcast),4000)
 $packet = \[byte\[\]\](,0xFF * 6)
 $packet += $MACAddr * 16
 \[void\] $UDPclient.Send($packet, $packet.Length)
 write "Wake-On-Lan magic packet sent to $MACAddrString, length $($packet.Length)"

I saved it as wol.ps1. You need to make sure you enable script execution in your PowerShell environment:

 


PS D:Documentsscripts> Set-ExecutionPolicy unrestricted

 Execution Policy Change
 The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose
 you to the security risks described in the about_Execution_Policies help topic. Do you want to change the execution
 policy?
 \[Y\] Yes \[N\] No \[S\] Suspend \[?\] Help (default is "Y"): Y

And then, to turn on my wife's machine was just


PS D:Documentsscripts> .wol.ps1 D8:D3:85:9A:98:60
 Wake-On-Lan magic packet sent to D8:D3:85:9A:98:60, length 102

No more walking upstairs for me. You can customize the script to check for - instead of : by replacing the .split(':') to .split('-') in the code. You can also change it to any other combination that works for you.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like