Jul 7 2011 2:18PM GMT
Posted by: Richard Siddaway
Disks
Next drive letter
Posted by: Richard Siddaway
I’ve been working on using the Hyper-V PowerShell library and wanted to use the Mount-VHD function. It wants a drive letter. The library provides Get-FirstAvailableDriveLetter but what I want is actually the next letter in the sequence. I want to avoid A & B to avoid confusion. So I needed a function to get the next drive letter
function get-nextdriveletter { $disk = Get-WmiObject -Class Win32_LogicalDisk | sort DeviceId -Descending | select -First 1 -Property DeviceID $letter = ($disk.DeviceID).Substring(0,1).ToUpper() if ($letter -eq "Z"){ Write-Host "No more drive letters available" } else { $nextletter = [char](([byte][char]$letter) + 1) $nextletter } }
Use WMI to get the last letter used – descending sort on DeviceID produces that. Take the letter, convert to a byte value, add 1 and convert back
Job done




