Wake On LAN is a feature that allows computers to stay in a low-energy standby mode until a magic packet arrives over the network - in which case these machines wake up and are ready for action. PowerShell does not ship a Wake On LAN cmdlet so many users resort to external tools such as WOL.EXE.
This is where PowerShell shines! All you need is to tell PowerShell what you want: how a Magic Packet is composed, and how it’s sent off.
In no time, you get your own brand new Wake On LAN command - no need anymore for external dependencies to Wake On LAN-tools in your code.
New Wake-On-LAN Command
Invoke-WakeOnLan
takes one or more MAC addresses, composes the Magic Packet and sends it to the machines:
function Invoke-WakeOnLan
{
param
(
# one or more MACAddresses
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
# mac address must be a following this regex pattern:
[ValidatePattern('^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$')]
[string[]]
$MacAddress
)
begin
{
# instantiate a UDP client:
$UDPclient = [System.Net.Sockets.UdpClient]::new()
}
process
{
foreach($_ in $MacAddress)
{
try {
$currentMacAddress = $_
# get byte array from mac address:
$mac = $currentMacAddress -split '[:-]' |
# convert the hex number into byte:
ForEach-Object {
[System.Convert]::ToByte($_, 16)
}
#region compose the "magic packet"
# create a byte array with 102 bytes initialized to 255 each:
$packet = [byte[]](,0xFF * 102)
# leave the first 6 bytes untouched, and
# repeat the target mac address bytes in bytes 7 through 102:
6..101 | Foreach-Object {
# $_ is indexing in the byte array,
# $_ % 6 produces repeating indices between 0 and 5
# (modulo operator)
$packet[$_] = $mac[($_ % 6)]
}
#endregion
# connect to port 400 on broadcast address:
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
# send the magic packet to the broadcast address:
$null = $UDPclient.Send($packet, $packet.Length)
Write-Verbose "sent magic packet to $currentMacAddress..."
}
catch
{
Write-Warning "Unable to send ${mac}: $_"
}
}
}
end
{
# release the UDF client and free its memory:
$UDPclient.Close()
$UDPclient.Dispose()
}
}
The function fully supports the PowerShell pipeline and can wake one or more computers.
Examples
Before you can wake a remote computer, you need to know its MAC address.
Identifying MAC Addresses
You can use WMI to find it out. The WMI class Win32_NetworkAdapter represents all network adapters in a computer.
This line lists the MAC address from all network adapters that are currently connected to a network:
Get-CimInstance -Query 'Select * From Win32_NetworkAdapter Where NetConnectionStatus=2' | Select-Object -Property Name, Manufacturer, MacAddress
Use the parameter -ComputerName to retrieve the information remotely, provided you have the required Administrator privileges, and the target machine is correctly configured for remote management.
The result looks similar to this:
Name Manufacturer NetConnectionId MacAddress
---- ------------ --------------- ----------
Killer(R) Wi-Fi 6 AX1650s 160MHz Wireless Network Adapter (201D2W) Intel Corporation WLAN 24:EE:9A:54:1B:E5
USB Ethernet DisplayLink Ethernet 4 98:E7:43:B5:B2:2F
Sending Magic Packet
Provided you ran the code above to define Invoke-WakeOnLan
, you can now send off a Magic Package to wake up the computer of choice. Simply submit its MAC address:
Invoke-WakeOnLan -MacAddress '24:EE:9A:54:1B:E5'
You can also wake a number of machines. Either submit the MAC addresses as a comma-separated list:
Invoke-WakeOnLan -MacAddress '24:EE:9A:54:1B:E5', '98:E7:43:B5:B2:2F' -Verbose
Or pipe the information:
'24:EE:9A:54:1B:E5', '98:E7:43:B5:B2:2F' | Invoke-WakeOnLan -Verbose
Considerations
To wake up a remote computer, it needs to be configured correctly. Typically, Wake on LAN must be enabled in the BIOS settings, and the network adapter and its power states need to support Wake on LAN as well.