Powershell WOL script

Script to wake a computer via network using Windows Powershell.

# Script to wake up a computer over network.

param(
[string]$mac = ’00:00:00:00:00:00′ #address of the network card (MAC address)
)

#checks the syntax of MAC address
if (!($mac -like “*:*:*:*:*:*”) -or ($mac -like “*-*-*-*-*-*”)){
write-error “mac address not in correct format”
break
}

#build magic package http://en.wikipedia.org/wiki/Wake-on-LAN#Magic_packet
$string=@($mac.split(“:””-“) | foreach {$_.insert(0,”0x”)})
$target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5])
# The magic packet is a broadcast frame containing anywhere within its payload 6 bytes of all 255 (FF FF FF FF FF FF in hexadecimal)
$packet = [byte[]](,0xFF * 102)
# followed by sixteen repetitions of the target computer’s 48-bit MAC address, for a total of 102 bytes.
6..101 |% { $packet[$_] = $target[($_%6)]}

# .NET framework lib para sockets
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$UDPclient.Send($packet, $packet.Length) | out-null

Batch FOR loop and variable modifiers

Write a FOR loop such as below

@echo off
for /R “C:\Users\Admin\Desktop” %%I in (*.*) do (
echo %%~nI
)
pause

To modifier the variable I, use the following

%~I Expands %I which removes any surrounding quotation marks (“”).
%~fI Expands %I to a fully qualified path name.
%~dI Expands %I to a drive letter only.
%~pI Expands %I to a path only.
%~nI Expands %I to a file name only.
%~xI Expands %I to a file extension only.
%~sI Expands path to contain short names only.
%~aI Expands %I to the file attributes of file.
%~tI Expands %I to the date and time of file.
%~zI Expands %I to the size of file.