Created
June 4, 2024 21:59
-
-
Save VibroAxe/4d7fd3465b9b9642b4a01742c9fa111e to your computer and use it in GitHub Desktop.
Copy to USB Stick
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#### Copies all files from current folder (exluding PS1 & BAT files) onto the next inserted USB Stick. Also clears existing files from stick | |
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange | |
try { | |
write-host (get-date -format s) " Beginning USB Flash Script..." | |
write-host (get-date -format s) " " | |
write-host (get-date -format s) " #############################################" | |
write-host (get-date -format s) " # WARNING #" | |
write-host (get-date -format s) " #############################################" | |
write-host (get-date -format s) " # The next usb stick inserted into this #" | |
write-host (get-date -format s) " # computer (whilst the script is running) #" | |
write-host (get-date -format s) " # will have all files deleted and replaced #" | |
write-host (get-date -format s) " # with the contents of this folder!! #" | |
write-host (get-date -format s) " #############################################" | |
write-host (get-date -format s) " " | |
write-host (get-date -format s) " " | |
while (1) { | |
write-host (get-date -format s) " Waiting for new drive (Press Ctrl+C to finish)" | |
do{ | |
$newEvent = Wait-Event -SourceIdentifier volumeChange | |
$eventType = $newEvent.SourceEventArgs.NewEvent.EventType | |
$eventTypeName = switch($eventType) | |
{ | |
1 {"Configuration changed"} | |
2 {"Device arrival"} | |
3 {"Device removal"} | |
4 {"docking"} | |
} | |
if ($eventType -eq 2) | |
{ | |
$driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName | |
$driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeName | |
write-host (get-date -format s) " Detected new drive " $driveLetter | |
break; | |
} | |
Remove-Event -SourceIdentifier volumeChange | |
} while (1-eq1) #Loop until next event | |
write-host (get-date -format s) " Deleting data from " $driveLetter | |
Start-Sleep -seconds 2 | |
$rmPath=$driveLetter[0]+$driveletter[1]+"/*" | |
rm $rmPath | |
write-host (get-date -format s) " Copying Data to " $driveLetter | |
Copy-Item * $driveLetter -Recurse -Exclude *.ps1,*.bat | |
Start-Sleep -seconds 2 | |
$driveEject = New-Object -comObject Shell.Application | |
$driveEject.Namespace(17).ParseName($driveLetter[0]+$driveletter[1]+"\").InvokeVerb("Eject") | |
write-host (get-date -format s) " Done " | |
write-host (get-date -format s) " Waiting for removal of " $driveLetter | |
do{ | |
$newEvent = Wait-Event -SourceIdentifier volumeChange | |
$eventType = $newEvent.SourceEventArgs.NewEvent.EventType | |
$eventTypeName = switch($eventType) | |
{ | |
1 {"Configuration changed"} | |
2 {"Device arrival"} | |
3 {"Device removal"} | |
4 {"docking"} | |
} | |
if ($eventType -eq 3) { | |
break; | |
} | |
Remove-Event -SourceIdentifier volumeChange | |
} while (1-eq1) #Loop until next event | |
} | |
} finally { | |
Unregister-Event -SourceIdentifier volumeChange | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment