Asked by:
Get the current usb drive letter

Question
-
Hello
I'm have a tough time trying to get the drive letter of a usb.
I am using the ISE in Admin mode
I have a ps script that resides on a usb drive. In the script, I'm trying to set the relative path to specific files (relative to the root of the USB it resides on).
The reason I need to do this is that when I go from work station to work station (as needed), I can simply run the script and have it copy the appropriate files to the appropriate place (from the usb to a specific path on the hd).
Because on each system, the usb is apt to get a different drive letter, I can't hard code the drive letter in the script.
Any help would be appreciated.
Thank you
Terry
Tuesday, June 11, 2019 8:33 PM
All replies
-
Look for the drive label.
\_(ツ)_/
Tuesday, June 11, 2019 9:12 PM -
Hi Terry,
you can run the following cmd command:
wmic logicaldisk where drivetype=2 get deviceid
in PowerShell and then it will look like this:
cmd /c "wmic logicaldisk where drivetype=2 get deviceid"
and this will show you the USB drives and their letters...In all cases you have an challenge: how to determine which drive to target if you have more then one USB drive... but this cannot be solved technically
Hope I could help. Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
- Edited by Stoyan ChalakovMVP Wednesday, June 12, 2019 12:56 PM
- Proposed as answer by LeeSeenLiMicrosoft contingent staff Tuesday, July 9, 2019 7:44 AM
Wednesday, June 12, 2019 12:31 PM -
It sure can be solved technically.
Get-PsDrive | Where Description -eq 'My USB label'
You can also set an event on the USB resource or scan the event log for the latest USB mount.
\_(ツ)_/
Wednesday, June 12, 2019 12:45 PM -
Agree, you can do that, but only if you have a uniform naming convention for those :)
Regards,
(Please take a moment to "Vote as Helpful" and/or "Mark as Answer" where applicable. This helps the community, keeps the forums tidy, and recognizes useful contributions. Thanks!) Blog: https://blog.pohn.ch/ Twitter: @StoyanChalakov
Wednesday, June 12, 2019 1:06 PM -
The Drive label may not work for me in some cases.
I did find a 1 liner by Marco Nuijens that led me to a solution:
$USBDrive = get-psdrive |where {$_.Root -match ":"} |% {if (Test-Path ($_.Root + "Do Not Delete")){$_.Root}}
I created a folder called "Do Not Delete" on my thumb drive. The above looks for it and returns the drive letter.
So, in my application, Im using it as such:
$IconPath = -Join ($USBDrive, 'ICON ICO\ICON.txt') Copy-Item $IconPath "C:\users\default\App Data\Microsoft\Windows\Shell"
Seems to be working as I need it to.
Thank You
Terry
- Proposed as answer by Stoyan ChalakovMVP Thursday, June 13, 2019 7:03 AM
Wednesday, June 12, 2019 2:27 PM -
(get-volume | where drivetype -eq removable).driveletter
- Edited by JS2010 Friday, June 14, 2019 2:35 PM
Friday, June 14, 2019 2:28 PM