Change driveletter of CD-rom

In a deployment using Altiris (or any other deployment utility) it is common to change driveletters. Especially when servers are reinstalled once in a while, you probably encounter problems doing so using diskpart.
Diskpart is the utility supplied with Windows by Microsoft which enables you to manage you disks, partitions and volumes. One limitation is, however, that you’re unable to select a specific drive using filters. For instance a CD-rom drive.

In an initial deployment you know how many disks there are and how many volumes. Because these are static you can create the diskpart script easily by specifying the disk ID or volume ID, which is numbered in the order of presence.
However, when you redeploy that same server this might be changed. If you’ve added a disk (or formatted it) the order of presence might be changed and the script will fail to execute the way you expected it.

In order to “solve” this problem I wrote the following replacement batch script which sets the drive letter of the CD-Rom to the letter Z:. You can change the driveletter, filter or script to your needs.

@Echo Off
REM Name : ChangeCDromDriveLetter.cmd
REM Author : I. Verheij - PepperByte
REM Description : Assigns a new drive letter to the last CD-rom found
REM Version : 0.1, 16-09-2010, Initial version
REM ------------------------------------------------------

REM Define the new drive letter of the CD-Rom
SET NewDrive=Z

REM Lookup the drive letter of the CD-Rom
FOR /F “tokens=2 delims==” %%A IN (‘wmic cdrom get drive /format:list^|find “=”‘) DO SET CurrentDrive=%%A

REM Lookup the volume assigned to this driveletter
ECHO LIST VOLUME>”%Temp%DiskpartListVolume”
FOR /F “tokens=2,3 delims= ” %%A IN (‘diskpart /s “%Temp%DiskpartListVolume”^|find “olume”‘) DO If %%B==%CurrentDrive:~0,1% SET CDRomVolume=%%A
DEL /Q “%Temp%DiskpartListVolume”
ECHO Volume = %CdRomVolume%

REM Assign the newe driveletter with DISKPART
ECHO SELECT VOLUME %CDRomVolume% >”%TEMP%DiskPartChangeLetter”
ECHO ASSIGN LETTER=%NewDrive:~0,1%>>”%TEMP%DiskPartChangeLetter”
CALL DISKPART.EXE /S “%TEMP%DiskPartChangeLetter”>”%TEMP%DiskPartOutput”
DEL /Q “%TEMP%DiskPartChangeLetter”
DEL /Q “%TEMP%DiskPartOutput”

REM Inform user
Echo The drive letter of the CD-Rom drive (volume %CDRomVolume%) has been changed from %CurrentDrive% to %NewDrive%:.

Edit 27-10-2010 : Remko Weijnen points me to his “Change Driveletter Commandline Tool” which gives the same result but without scripting.

Ingmar Verheij

Was once an enthusiastic PepperByte employee but is now working at Citrix. His blogs are still valuable to us and we hope to you too.