0

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?

Mofi
  • 46,139
  • 17
  • 80
  • 143
user2818170
  • 107
  • 1
  • 10
  • 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 Answers2

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
1
$driveletter = (Get-Volume -FileSystemLabel "USBData").DriveLetter
echo "${driveletter}:\"
Anders
  • 97,548
  • 12
  • 110
  • 164