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

Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015