Eric Bergman-Terrell's Blog

PowerShell Tip: Determine the Drive Letter Corresponding to a Volume Name
August 11, 2011

I have an external USB drive that I use for backups. The backups are performed by a PowerShell script. My problem is that the drive letters change frequently, as other USB drives are plugged in and unplugged.

I updated the PowerShell script to determine the drive letter based on the volume name, which does not change. Here is the code that does this:

function GetDriveLetter($volume_name)
{
$drive = Get-WmiObject win32_logicaldisk | Where-Object {$_.VolumeName-eq $volume_name } | select -ExpandProperty DeviceID

if ($drive -eq $null)
{
Throw "Cannot find drive with volume name of " + $volume_name
}

return $drive
}
Keywords: PowerShell, USB Drive, Drive Letter, Volume Name

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021