Skip to content

Instantly share code, notes, and snippets.

@LuMarans30
Last active January 23, 2023 17:13
Show Gist options
  • Select an option

  • Save LuMarans30/c4e559542357c88fe22763ce99c10f91 to your computer and use it in GitHub Desktop.

Select an option

Save LuMarans30/c4e559542357c88fe22763ce99c10f91 to your computer and use it in GitHub Desktop.
A simple powershell script to turn on a computer in the same network knowing its MAC address (you must enable the wake on lan function in the BIOS/UEFI of the target).
$Mac = $null
if ($args.count -eq 1 ) {
$Mac = $args[0]
$Mac | out-file -filepath .\mac.txt -width 200
Write-Output "The next time you won't have to re-enter the MAC address because it has been saved on a file in this directory."
}
else{
$file_data = Get-Content .\mac.txt
if ( $file_data -eq $null ) {
Write-Output "error: please enter a MAC address"
exit
}
else{
$Mac = $file_data
Write-Output "The previously used MAC adress is $Mac"
}
}
$MacByteArray = $Mac -split "[:-]" | ForEach-Object { [Byte] "0x$_"}
[Byte[]] $MagicPacket = (,0xFF * 6) + ($MacByteArray * 16)
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7)
$UdpClient.Send($MagicPacket,$MagicPacket.Length)
$UdpClient.Close()
@LuMarans30
Copy link
Author

This is my first powershell script, please leave a comment if you notice any possible bugs or optimizations. Thanks! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment