Hey, Scripting Guy! Question

Hey, Scripting Guy! Given the drive letter, how can I determine the drive label?

-- PM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PM. You know, after sitting around all winter, checking his watch every 15 minutes or so just to verify the date and time, the Scripting Guy who writes this column is pleased to announce that baseball season is officially here. Once more he can enjoy the Great American Pastime. Once more he can help the youth of America learn the value of teamwork and sportsmanship. Most important of all, once more he can leave work an hour or two early every day either to go coach his team or to watch his son play for the local high school team.

And you wondered why he likes baseball so much.

In other words, our Scripting Columnist is in a good mood today. Of course, that might not last long: after all, this year’s Kirkland Fire team is unlikely to annihilate all the other teams in the league, like they did last season. Today, however, we’re feeling good, and to celebrate we’ve decided to not only answer your question, but to answer a second question, one you didn’t even ask. Is that service or what?

First things first. You’d like to know how, given a drive letter, you can determine the drive label for a drive. Well, how about by using a script like this one:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DeviceID = 'D:'")

For Each objItem in colItems
    Wscript.Echo objItem.VolumeName 
Next

And, yes, it is a very simple little script. (Hey, it’s baseball season: we don’t have time for writing scripts and otherwise doing our work!) We start off by connecting to the WMI service on the local computer; we could also perform this same chore on a remote machine just by assigning the name of that computer to the variable strComputer. We then use this line of code to return a collection of all the drives on the computer that have a DeviceID of D:

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DeviceID = 'D:'")

Because drive letters must be unique on a computer we know that our collection will have, at most, just one item: drive D (which is just what we wanted). To determine the drive label all we have to do is set up a For Each loop to walk us through this one-item collection; inside that loop we echo back the value of the VolumeName property:

Wscript.Echo objItem.VolumeName

That’s it. If you want to switch this around - that is, given a drive label determine the DeviceID - well, all you have to do is run this script:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where VolumeName = 'Data Drive'")

For Each objItem in colItems
    Wscript.Echo objItem.DeviceID 
Next

Cool, huh?

But wait: don’t go yet. After all, that was just a third bonus answer on top of the bonus answer we promised to give you (you know, the answer to the question you never even asked). Having solved the problem of determining the drive label for a drive the next logical question is this: can I use a script to change the drive label for a drive? If it wasn’t baseball season this would probably be our answer: “No, you can’t. Now go away and leave us alone.” But seeing as how this is baseball season our answer is a little different: “Of course you can do this. And we’d be more than happy to show you how.”

In fact, here’s how.

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_LogicalDisk Where DeviceID = 'D:'")

For Each objItem in colItems
    objItem.VolumeName = "Data Drive"
    objItem.Put_
Next

As you can see, this script is remarkably similar to the first script we showed you. In fact, the only difference is what goes on inside the For Each loop:

objItem.VolumeName = "Data Drive"
objItem.Put_

What are we doing this time around? Well, this time around we’re assigning a new drive label (in this case, Data Drive) to the VolumeName property; we can do that because VolumeName happens to be a read-write value. After that we simply call the Put_ method (note the underscore at the end of the method name) to write the changes to the drive. Drive D will now have a brand-new label: Data Drive. It just doesn’t get any better than that, does it?

And now, if you’ll excuse us, we have a … meeting … to go to. Right. A meeting ….