I have a Powershell script on my windows machine that needs to access data from a USB drive. It reads a file from the USB stick and does its thing. I need to do this over many machines. The problem is that the USB drive can appear under different drive letters, depending on the machine I insert the stick into. My USB drive is called "USBData". Is there a way to reliably access the USB drive using its name rather than its drive letter?
Asked
Active
Viewed 748 times
0
-
It is not really written but it looks like the executed PowerShell script file is on the USB storage media mounted as USB drive by Windows on being plugged in. In this case please see [What's the best way to determine the location of the current PowerShell script?](https://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script) and [How can I get the file system location of a PowerShell script?](https://stackoverflow.com/questions/3667238/how-can-i-get-the-file-system-location-of-a-powershell-script) – Mofi Feb 05 '22 at 14:51
2 Answers
1
You can do a:
(Get-WMIObject Win32_Volume | ? { $_.Label -eq 'USBData' }).DriveLetter
to get the Drive and then execute relative to it. Something like:
$USBDrive = (Get-WMIObject Win32_Volume | ? { $_.Label -eq 'USBData' }).DriveLetter
$ProcessFullPath = "$USBDrive\Executable.exe"
Start-Process $ProcessFullPath

OttScott
- 21
- 4
-
Yes, if you change the command to Start-Process, it works. So this is nearly the right answer. – user2818170 Feb 05 '22 at 19:43
-
-
Start-Process is correct. I was typing from memory. @Anders has the better answer since it doesn't need inline filtering. – OttScott Feb 06 '22 at 18:10
1
$driveletter = (Get-Volume -FileSystemLabel "USBData").DriveLetter
echo "${driveletter}:\"

Anders
- 97,548
- 12
- 110
- 164