Koldov Posted February 10, 2023 Posted February 10, 2023 (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 February 10, 2023 by Koldov
skyline_17 Posted February 10, 2023 Posted February 10, 2023 You could use this to force the the drive to mount to the same drive letter https://www.uwe-sieber.de/usbdlm_e.html 2
CHiLL Posted February 10, 2023 Posted February 10, 2023 $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. 2
RLR Posted February 10, 2023 Posted February 10, 2023 (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 February 10, 2023 by RLR 2
andy_b Posted February 10, 2023 Posted February 10, 2023 Give the USB drive a unique label than then use this ? $DriveLetter = Get-Volume -FileSystemLabel 'UniqueLabel' | % DriveLetter 2
bald_pig Posted February 10, 2023 Posted February 10, 2023 If the script is on the drive, can you not just use a relative path? 2
Norphy Posted February 11, 2023 Posted February 11, 2023 Put the script into the folder where the files are, and use %~dp0 instead of the folder in the path? 1
XiJ Posted February 11, 2023 Posted February 11, 2023 $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. 1
Roberto Posted February 11, 2023 Posted February 11, 2023 Is the script on the usb drive being mounted? If so, look at $MyInvocation? https://www.tutorialspoint.com/how-to-get-the-path-of-the-currently-executing-script-in-powershell 1
Koldov Posted February 21, 2023 Author Posted February 21, 2023 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\
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now