Avatar of izharkhan
izharkhan
Flag for United States of America asked on

script to change DVD Drive letter

Hi,
I need a script to change a DVD Drive letter to R: in multiple servers. we have windows 2008 and windows 2003 servers. script should find DVD Drive and then change its letter to R:
In some of the servers we have D: and E: letters for DVD Drive and need to change them to R:
Thanks
Izhar
PowershellVB ScriptWindows Server 2008

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
chubby_informer

RobSampson

Hi, if you have a test server, run this code on that first, but it should work.  It uses WMI to identify drives with a DriveType of 5 (Compact Disc), and changes the letter to R:

This runs locally only, it does not run remotely, because the only way (or at least the most reliable way) to change the drive letter was to use the local MountVol utility.

You could run it remotely using PSExec though.

Regards,

Rob.

ChangeDriveLetters

Sub ChangeDriveLetters
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colVolumes = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType=5")
For Each objDisk in colVolumes
    If objDisk.DeviceID <> "R:" Then ChangeDriveLetterWithMountvol objDisk.DeviceID, "R:"
Next
End Sub

' Change the drive in one drive letter to another drive letter using
' mountvol.exe tool
'
Sub ChangeDriveLetterWithMountvol(strSourceDrive, strTargetDrive) 

    Dim objShell, objExec
    Dim strVolume
    
    Set objShell = CreateObject("WScript.Shell")
    
    ' Get volume associated to the old drive letter.
    '
    Set objExec = objShell.Exec("mountvol " & strSourceDrive & " /L")
    strVolume = Trim(objExec.StdOut.ReadLine())
    While objExec.Status = 0
        WScript.Sleep(100)
    Wend
    
    ' Unmount the drive.
    '
    Set objExec = objShell.Exec("mountvol " & strSourceDrive & " /D")
    While objExec.Status = 0
        WScript.Sleep(100)
    Wend
    
    ' Mount the drive on the new drive letter.
    '
    Set objExec = objShell.Exec("mountvol " & strTargetDrive & " " & strVolume)
    While objExec.Status = 0
        WScript.Sleep(100)
    Wend

End Sub 

Open in new window

izharkhan

ASKER
Hi Rob,
I tried this script but its not working.
Thanks
Izhar
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER CERTIFIED SOLUTION
RobSampson

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question