Skip to content

Instantly share code, notes, and snippets.

@alimbada
Last active February 8, 2024 21:03
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save alimbada/4949168 to your computer and use it in GitHub Desktop.
Save alimbada/4949168 to your computer and use it in GitHub Desktop.
PowerShell script for sending Wake On LAN magic packets to given machines/MAC address(es)
#######################################################
##
## Wake.ps1, v1.0, 2013
##
## Adapted by Ammaar Limbada
## Original Author: Matthijs ten Seldam, Microsoft (see: http://blogs.technet.com/matthts)
##
#######################################################
<#
.SYNOPSIS
Starts a list of physical machines by using Wake On LAN.
.DESCRIPTION
Wake sends a Wake On LAN magic packet to a given machine's MAC address.
.PARAMETER MacAddress
MacAddress of target machine to wake.
.EXAMPLE
Wake A0DEF169BE02
.INPUTS
None
.OUTPUTS
None
.NOTES
Make sure the MAC addresses supplied don't contain "-" or ".".
#>
param( [Parameter(Mandatory=$true, HelpMessage="MAC address of target machine to wake up")]
[string] $MacAddress )
Set-StrictMode -Version Latest
function Send-Packet([string]$MacAddress)
{
<#
.SYNOPSIS
Sends a number of magic packets using UDP broadcast.
.DESCRIPTION
Send-Packet sends a specified number of magic packets to a MAC address in order to wake up the machine.
.PARAMETER MacAddress
The MAC address of the machine to wake up.
#>
try
{
$Broadcast = ([System.Net.IPAddress]::Broadcast)
## Create UDP client instance
$UdpClient = New-Object Net.Sockets.UdpClient
## Create IP endpoints for each port
$IPEndPoint = New-Object Net.IPEndPoint $Broadcast, 9
## Construct physical address instance for the MAC address of the machine (string to byte array)
$MAC = [Net.NetworkInformation.PhysicalAddress]::Parse($MacAddress.ToUpper())
## Construct the Magic Packet frame
$Packet = [Byte[]](,0xFF*6)+($MAC.GetAddressBytes()*16)
## Broadcast UDP packets to the IP endpoint of the machine
$UdpClient.Send($Packet, $Packet.Length, $IPEndPoint) | Out-Null
$UdpClient.Close()
}
catch
{
$UdpClient.Dispose()
$Error | Write-Error;
}
}
## Send magic packet to wake machine
Write "Sending magic packet to $MacAddress"
Send-Packet $MacAddress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment