Change the drive letter of a removable disk

RecipeSometimes it may be necessary to instruct your system not to use a specific drive letter for use by removable storage units like USB memory sticks, card readers or hard drives. As an example, your setup may contain network drive mappings relying on availability of specific letters. The Visual Basic script below takes a drive letter as a parameter, checks if it is mounted as a removable disk and move the letter to the next available letter.


cscript /NoLogo change-drive-letter.vbs drive-letter




' Change the Drive Letter of a removable volume with the next available letter

' Specify drive letter as a parameter
If WScript.Arguments.Count = 0 Then
 Wscript.Echo "This script requires the drive letter to be released as a parameter."
 strDrive = WScript.Arguments.Item(0)
End If

strComputer = "."

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

' Drive type - 2:removable, 3:local, 4:network
Set colVolumes = objWMIService.ExecQuery _
    ("Select * from Win32_Volume Where Name = '" & strDrive & ":\\' And DriveType = 2")

set objFS=CreateObject ("Scripting.FileSystemObject")

For Each objVolume in colVolumes

 'Find out next available drive letter
 set colDrives=objFS.Drives
 letter = Asc(Ucase(strDrive)) + 1
 For i = letter to Asc("Z")
  If objFS.DriveExists(Chr(letter) & ":") Then
'   Wscript.Echo "The next available drive letter is " & UCASE(Chr(letter)) & ":"
   objVolume.DriveLetter = UCASE(Chr(letter)) & ":"
  End If
 Wscript.Echo "There are no available drive letters after " & strDrive & " on this computer."