To wake a PC up just run the following:
PS C:\PS> Wake-OnLan -BroadCastAddress 192.168.0.255 -MacAddress 32:2D:F4:32:7D:E5
Where -BroadCastAddress is the IP Address subnet address and where -MacAddress is the mac address of the computer to wake up. The -MacAddress parameter takes the address in either the colon (32:24:F4:32:7D:E5) or the dash (32-24-F4-32-7D-E5) format. Here is the function...
Function Wake-OnLan {
param(
[string]$BroadCastAddress=$(throw "Mandatory -Parameter -BroadCastAddress missing, for example 192.168.0.255"),
[string]$MacAddress=$(throw "Mandatory -Parameter -MacAddress missing, for example 32:2D:F4:32:7D:E5 or 32-2D-F4-32-7D-E5")
)
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Net")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Net.Sockets")
$udpClient = new-object System.Net.Sockets.UdpClient
$endPoint = new-object System.Net.IPEndPoint $([System.Net.IPAddress]::Parse($BroadCastAddress)),10000
$MacAddress = $MacAddress.Replace(":","-")
[byte[]]$macBytes = $macAddress.split("-") | %{[byte]"0x$_"}
[byte[]]$bytes = new-object "byte[]" $(6 + 16 * $($macBytes.length))
for ($i = 0; $i -lt 6; $i++) {
$bytes[$i] = [byte] 0xff
}
for ($i = 6; $i -lt $bytes.length; $i += $macBytes.length) {
for($j = 0; $j -lt $macBytes.length; $j++){
$bytes[$i + $j] = $macBytes[$j]
}
}
$udpClient.connect($endPoint)
[void]$udpClient.Send($bytes,$bytes.length)
Write-Host "Magic packet has been sent"
}
param(
[string]$BroadCastAddress=$(throw "Mandatory -Parameter -BroadCastAddress missing, for example 192.168.0.255"),
[string]$MacAddress=$(throw "Mandatory -Parameter -MacAddress missing, for example 32:2D:F4:32:7D:E5 or 32-2D-F4-32-7D-E5")
)
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Net")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Net.Sockets")
$udpClient = new-object System.Net.Sockets.UdpClient
$endPoint = new-object System.Net.IPEndPoint $([System.Net.IPAddress]::Parse($BroadCastAddress)),10000
$MacAddress = $MacAddress.Replace(":","-")
[byte[]]$macBytes = $macAddress.split("-") | %{[byte]"0x$_"}
[byte[]]$bytes = new-object "byte[]" $(6 + 16 * $($macBytes.length))
for ($i = 0; $i -lt 6; $i++) {
$bytes[$i] = [byte] 0xff
}
for ($i = 6; $i -lt $bytes.length; $i += $macBytes.length) {
for($j = 0; $j -lt $macBytes.length; $j++){
$bytes[$i + $j] = $macBytes[$j]
}
}
$udpClient.connect($endPoint)
[void]$udpClient.Send($bytes,$bytes.length)
Write-Host "Magic packet has been sent"
}
Please enjoy!