简体   繁体   中英

Can I access a USB drive by its name?

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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM