How to Send a Wake-on-LAN (WOL) Magic Packet in Batch Script
Wake-on-LAN (WOL) is a networking standard that allows a computer to be turned on or "woken up" by a network message. This is an incredibly powerful tool for automation, allowing a script to power on a remote machine (like a file server or a workstation) before performing a task, such as running a backup, installing updates, or accessing a shared resource.
This guide will explain the critical hardware prerequisites for WOL and teach you how to send a "Magic Packet" from a batch script. Since cmd.exe has no native ability to construct and send the special network packet required, we will use the modern and recommended method of calling a PowerShell one-liner from our batch script.
What is Wake-on-LAN and the "Magic Packet"?
WOL works when a network adapter, even on a powered-down computer, stays in a low-power listening mode. It listens for a special network broadcast called a Magic Packet. This packet contains a specific sequence of bytes, including the MAC Address of the target computer's network card repeated 16 times. When the network card sees a Magic Packet intended for it, it signals the computer's motherboard to power on.
CRITICAL: Hardware and BIOS Prerequisites
A WOL script is the easiest part of the process. Making it work depends entirely on the configuration of the target computer (the one you want to wake up). If these steps are not completed, no script will ever work.
- Enable WOL in the BIOS/UEFI: You must enter the target computer's BIOS/UEFI setup utility (usually by pressing
Del,F2, orF12during boot). You need to find and enable an option often called:Wake on LANPower On by PCIE DeviceResume by Onboard LAN
- Enable WOL in Windows Device Manager:
- On the target machine, open Device Manager.
- Find your network adapter, right-click it, and go to
Properties. - Go to the
Power Managementtab and check the box that says "Allow this device to wake the computer." You may also need to check "Only allow a magic packet to wake the computer." - Go to the
Advancedtab and find the "Wake on Magic Packet" property. Make sure it is set toEnabled.
- Local Network: The computer sending the packet and the target computer must be on the same local network (subnet).
The Core Method (Recommended): Using PowerShell
The only reliable, built-in way to send a Magic Packet from a batch script is to delegate the task to PowerShell, which can construct and send the necessary UDP network packets.
This command is long, but it can be called directly from your batch file. It takes a MAC address as input and sends the packet. See the example below.
Basic Example: A Simple Wake-Up Script
This script defines the MAC address of a target computer and calls the PowerShell command to send the Magic Packet.
@ECHO OFF
SETLOCAL
REM --- Configuration ---
REM The MAC address of the computer you want to wake up.
REM Remove all dashes, colons, or spaces.
SET "MAC_ADDRESS=001A2B3C4D5E"
ECHO --- Wake-on-LAN Sender ---
ECHO Targeting MAC: %MAC_ADDRESS%
ECHO Sending Magic Packet...
powershell -Command ^
"$mac = '%MAC_ADDRESS%'; " ^
"$macByteArray = $mac -split '([a-fA-F0-9]{2})' | ? { $_ } | % { [Convert]::ToByte($_, 16) }; " ^
"$magicPacket = ([byte[]](,0xFF) * 6) + ($macByteArray * 16); " ^
"$udpClient = New-Object System.Net.Sockets.UdpClient; " ^
"$udpClient.Connect(([System.Net.IPAddress]::Broadcast), 9); " ^
"$udpClient.Send($magicPacket, $magicPacket.Length) | Out-Null; " ^
"$udpClient.Close()"
ECHO.
ECHO --- Magic Packet sent successfully ---
ENDLOCAL
How the PowerShell Method Works
The PowerShell command, while complex-looking, performs a standard sequence of networking tasks:
$mac = ...: It takes the MAC address string from the batch variable.$macByteArray = ...: It converts the hex string (e.g.,001A2B...) into an actual array of bytes.$magicPacket = ...: This is the key step. It constructs the Magic Packet by creating a byte array with six0xFFbytes, followed by the target's MAC address byte array repeated 16 times.$udpClient = ...: It creates a UDP networking client.$udpClient.Connect(...): It connects the client to the network's broadcast address (255.255.255.255) on UDP port9(the standard for WOL).$udpClient.Send(...): It sends the Magic Packet to the entire local network.$udpClient.Close(): It cleans up the network connection.
Common Pitfalls and How to Solve Them
Problem: It Just Doesn't Work (The Prerequisites)
This is the cause of 99% of all WOL failures. The script sends the packet, but the target computer does nothing.
Solution: Go back and triple-check all the prerequisites on the target machine. Check the BIOS settings and the network adapter's driver properties in Windows.
Problem: Incorrect MAC Address Format
The PowerShell script expects a continuous string of hexadecimal characters with no separators (e.g., 001A2B3C4D5E). If you provide it with 00-1A-2B... or 00:1A:2B..., it will fail.
Solution: Your batch script should clean up the MAC address first using string substitution.
SET "MAC_WITH_DASHES=00-1A-2B-3C-4D-5E"
SET "MAC_CLEAN=%MAC_WITH_DASHES:-=%"
Problem: Waking a Computer Over the Internet
WOL is designed to work on a local area network (LAN). Sending a Magic Packet over the internet is very difficult and often impossible, as most internet routers do not forward the necessary broadcast packets.
Solution: This is beyond the scope of a simple script. It requires advanced network configuration (like a VPN or specific port forwarding rules) and is not a feature of WOL itself.
Practical Example: A "Wake My Server" Utility
This script provides a user-friendly menu to wake up one of several pre-configured computers.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
REM --- Define our list of servers ---
SET "servers[1].name=File Server"
SET "servers[1].mac=00-1A-2B-3C-4D-5E"
SET "servers[2].name=Gaming PC"
SET "servers[2].mac=A1-B2-C3-D4-E5-F6"
SET "ServerCount=2"
:Menu
CLS
ECHO --- Wake-on-LAN Menu ---
FOR /L %%i IN (1,1,%ServerCount%) DO (
ECHO %%i. !servers[%%i].name! (!servers[%%i].mac!)
)
ECHO.
CHOICE /C 12 /M "Choose a computer to wake up: "
SET "TargetMAC="
CALL SET "TargetMAC=%%servers[%ERRORLEVEL%].mac%%"
ECHO.
ECHO Waking up %TargetMAC%...
SET "CleanMAC=%TargetMAC:-=%"
powershell -Command "$mac='%CleanMAC%'; $macByteArray=$mac -split '([a-fA-F0-9]{2})'|?{$_}|%{[Convert]::ToByte($_,16)}; $p=([byte[]](,0xFF)*6)+($macByteArray*16); $c=New-Object System.Net.Sockets.UdpClient; $c.Connect(([System.Net.IPAddress]::Broadcast),9); $c.Send($p,$p.Length)|Out-Null; $c.Close()"
PAUSE
ENDLOCAL
Conclusion
While batch scripting cannot create a Wake-on-LAN packet by itself, it is a perfect "launcher" for a more powerful PowerShell command.
Key takeaways:
- WOL absolutely requires proper configuration in the BIOS and Windows Device Manager of the target computer. This is the most common point of failure.
- The PowerShell one-liner is the modern, recommended, and self-contained method for sending the Magic Packet.
- Ensure the MAC address is formatted as a clean string (e.g.,
001A2B3C4D5E) before passing it to the PowerShell command. - WOL is designed for use on a local network (LAN).