Home > database >  Can I access a USB drive by its name?
Can I access a USB drive by its name?

Time:02-05

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?

CodePudding user response:

$driveletter = (Get-Volume -FileSystemLabel "USBData").DriveLetter
echo "${driveletter}:\"

CodePudding user response:

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-PSProcess $ProcessFullPath
  •  Tags:  
  • Related