Jump to content

Powershell script to copy files from USB without knowing drive letter...?


Recommended Posts

Posted (edited)

TL;DR - What can I put in the code to make it pull files from the USB, regardless of drive letter...?

 

I've tried looking for an answer but can't seem to ask the right question for Google (or the answer I do get is for a complicated situation)...

 

But what I need is relatively simple (I hope)...

 

I have a USB which obviously changes drive letter depending on what computer it is plugged into.

 

I'm trying to copy multiple files from the USB with a click (or two), unfortunately they are referenced in the script by the letter of the USB.

 

Actually there's two very simple 'scripts', a 'copy to' and a 'copy from', but the 'copy to' the USB script is OK as the originating computer never changes drive letters and the paths to the various files remain constant.

 

However, the files need to be distributed to various other computers (they are not networked) and these have different hardware meaning the USB drive letter changes.

 

So if I had the lines:

 

Copy-Item "G:\FolderA\Folder1\Very_Important_File_1.vip" -Destination "C:\Users\Admin\Desktop" -Force
Copy-Item "G:\FolderB\Folder2\Very_Important_File_2.vip" -Destination "C:\Users\Admin\Desktop" -Force
Copy-Item "G:\FolderC\Folder3\Very_Important_File_3.vip" -Destination "C:\Users\Admin\Desktop" -Force
Copy-Item "G:\FolderD\Folder4\Very_Important_File_4.vip" -Destination "C:\Users\Admin\Desktop" -Force

 

What can I put in the script to refence the USB instead of 'G:' (obviously this works fine if the computer the USB is plugged into assigns G: but fails if it's anything else).

Edited by Koldov
Posted

$USB = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |  %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}

 

Source: https://stackoverflow.com/questions/10634396/get-the-drive-letter-of-usb-drive-in-powershell

 

That'll return the drive letter of the connected removable device. However, if you have more than one removable device connected, it'll return both drive letters and your script would fail.

  • Thanks 2
Posted (edited)

A slightly dirty way of doing it is adding all the possible drive letters to an array and looping through that with a test-path of one of the files that will only exist on your usb drive:

 

https://paste.ofcode.org/tJZGQkTVTuwYqPJRUuA6CT

 

Couldn't figure out how to paste code into the forum without it messing up the formatting and putting it all on one line (even when using the code tags).

Edited by RLR
  • Thanks 2
Posted
$USB = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |  %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}

 

Source: https://stackoverflow.com/questions/10634396/get-the-drive-letter-of-usb-drive-in-powershell

 

That'll return the drive letter of the connected removable device. However, if you have more than one removable device connected, it'll return both drive letters and your script would fail.

 

This is the way I’d have approached it.

 

Other (dirty) ways would be to check a certain file exists on the usb drive (could be a hidden file you’ve written), the label of the usb drive or loop through drive letters to see what is available.

You can use multiple ones of these of course.

  • Thanks 1
  • 2 weeks later...
Posted

Thanks all, sorry for the late reply...

 

Great suggestions all, I have put the script on the USB drive.

 

So in the end I used:

 

$PSScriptRoot\

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...