Simple USB Synchronisation script

Hey all!

I just made very plain and simple usb synchro script. it does NOT check the lastwritetime and update the folder, it is something i made for my boss and his specific wish was to just copy the files that were not in the destination folder. I still don’t know why he wanted it this way but meh… Maybe later if i have more time i’ll edit this post and update the script in order to copy the files based on last write time.

So how does it work? Very simple, the script will ask you which drive you want to “backup”. Select the drive and the script will save the DeviceId of that drive in your c:Temp folder. If nonexistent it will create it himself. It does that in order to know which drive it has to check and copy based on the times you preset in task scheduler. (You have to use this script with task scheduler offc in order to make it synch at certain times). I’m not sure if Start-bitstransfer would’ve been a better choice for this task since it is better at handling bigger files, i might change that in the future also.

I’m sure there are better ways for this and i would be glad to hear from em.

Thanks all! Have a good one!

P.s. – You have to run the script 2x if you run it the first time since it has to create a file. not sure how i can continue it yet since my skills arent all that good yet.

  • Also get-childitem returned NULL for the folder c:USB BACKUP so i had to create an empty folder in there in order to make it work.

EDIT: Since there were some performance issues i edited the script. should be faster now.

 cls $MsgIntro = @' ******************** ******************** Usb Synchronisation Made By Davy Dirkse ******************** ******************** '@ #Intro, obviously write-host -ForegroundColor Magenta "$MsgIntro" #Check if Temp Folder Exists, if not create it $CheckIfExistTemp = Test-Path "C:Temp" -ErrorAction SilentlyContinue $CheckIfExistUsbBackup = Test-path "C:Usb Backup" -ErrorAction SilentlyContinue if(!($CheckIfExistTemp)) { New-Item -ItemType Directory -path "C:Temp" } if(!($CheckIfExistUsbBackup)) { New-Item -ItemType Directory -Path "c:Usb Backup" New-Item -ItemType Directory -Path "C:Usb BackupDo Not Delete This Folder" } #Check if the File Usb Synchronisation.txt exists, if not, make user select the drive and store the information in the file. $CheckForFile = get-childitem -Path 'C:tempUsb Synchronisation.txt' -ErrorAction SilentlyContinue if (!($CheckForFile)) { #Get drive & DeviceId $WhichUsb = Get-WmiObject -class win32_volume $Fetch = $WhichUsb | select Driveletter, Label, DeviceId | Out-GridView -PassThru $Usb = $Fetch | select -ExpandProperty deviceid | out-file "c:TempUsb Synchronisation.txt" } else { #File Exists, load up the DeviceId $GetUsb = Get-Content 'C:tempUsb Synchronisation.txt' $Volume = Get-WmiObject -Class win32_volume | Where-Object { $_.DeviceId -eq "$GetUsb" } | select -ExpandProperty Caption #Compare the USB with the local folder and if necessary copy the files missing. $Ref = Get-ChildItem -Path "$Volume" -recurse -force -erroraction silentlycontinue $Diff = Get-ChildItem -Path 'C:Usb Backup' -Recurse -Force $Files = compare-object -ReferenceObject $Ref -DifferenceObject $diff | where-object { $_.sideindicator -eq "<=" } | Select-Object InputObject -ExpandProperty Inputobject ForEach($File in $Files) { $Files.FullName | Copy-Item -Destination "C:Usb Backup" -Container -Force -recurse -verbose } } 

submitted by /u/PRIdEVisions
[link] [comments]

Leave a Reply