Changing Logical Disk Volume Names

Microsoft® Windows® 2000 Scripting Guide

Using drive letters as the sole means of identifying a logical drive can cause confusion. Drive letters can vary not only from computer to computer (depending on how many drives are installed on each computer), but can also vary over time on an individual computer. For example, a computer might have a removable drive connected, and thus have drives A, B, C, D, and E. Later, the drive might be disconnected and the computer would then have drives A, B, C, and D.

One way to work around this problem is to use volume names (also referred to as volume labels) to help differentiate logical drives. For example, a computer might have the following logical drives:

  • Drive C (system drive)

  • Drive D (applications drive)

  • Drive E (data drive)

Instead of referring to these as drives C, D, and E, you can give them the volume names System, Applications, and Data. This makes it easier to identify the individual drives, particularly if other computers in the organization are configured in a different manner, for example, with drive D hosting data and drive E hosting applications. Using a consistent naming scheme across the organization helps facilitate administration and makes it easier for users who might use a different computer each time they log on.

You can use the Win32_LogicalDisk class to rename volumes on a computer. When renaming volumes, consider the following issues:

  • Volume names are limited to 32 characters.

  • You cannot change the volume label on a removable drive.

  • Volume names do not have to be unique on a computer. Therefore, if you specify the incorrect drive when using scripts to change volume names, you could inadvertently give every volume on the computer the same name.

Scripting Steps

Listing 10.5 contains a script that changes the name of a volume. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2 and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_DiskDrive class.

    To ensure that only drive C receives the name change, a Where clause is included to limit data retrieval to those drives with a DeviceID of C. The query thus returns a collection consisting of only those logical disk drives with a DeviceID of C.

  4. For the only disk drive in the collection, set the VolumeName property to "Finance Volume".

  5. Use the Put_ method to apply the changes and rename the volume.

Listing 10.5 Changing Volume Names


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDrives = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_LogicalDisk WHERE DeviceID = 'C:'")
For Each objDrive in colDrives
 objDrive.VolumeName = "Finance Volume"