{"id":67763,"date":"2006-03-14T15:13:00","date_gmt":"2006-03-14T15:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/14\/how-can-i-determine-the-drive-label-of-a-drive\/"},"modified":"2006-03-14T15:13:00","modified_gmt":"2006-03-14T15:13:00","slug":"how-can-i-determine-the-drive-label-of-a-drive","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-drive-label-of-a-drive\/","title":{"rendered":"How Can I Determine the Drive Label of a Drive?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Given the drive letter, how can I determine the drive label?<BR><BR>&#8212; PM<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>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.<\/P>\n<P>And you wondered <I>why<\/I> he likes baseball so much.<\/P>\n<P>In other words, our Scripting Columnist is in a good mood today. Of course, that might not last long: after all, this year\u2019s Kirkland Fire team is unlikely to annihilate all the other teams in the league, like they did last season. Today, however, we\u2019re feeling good, and to celebrate we\u2019ve decided to not only answer your question, but to answer a second question, one you didn\u2019t even ask. Is that service or what?<\/P>\n<P>First things first. You\u2019d 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:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where DeviceID = &#8216;D:'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.VolumeName \nNext\n<\/PRE>\n<P>And, yes, it <I>is<\/I> a very simple little script. (Hey, it\u2019s baseball season: we don\u2019t have time for writing scripts and otherwise doing our <I>work<\/I>!) 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 <B>DeviceID<\/B> of D:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where DeviceID = &#8216;D:'&#8221;)\n<\/PRE>\n<P>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 <B>VolumeName<\/B> property:<\/P><PRE class=\"codeSample\">Wscript.Echo objItem.VolumeName\n<\/PRE>\n<P>That\u2019s it. If you want to switch this around &#8211; that is, given a drive label determine the DeviceID &#8211; well, all you have to do is run <I>this<\/I> script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where VolumeName = &#8216;Data Drive'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo objItem.DeviceID \nNext\n<\/PRE>\n<P>Cool, huh?<\/P>\n<P>But wait: don\u2019t go yet. After all, that was just a third bonus answer on <I>top<\/I> 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 <I>change<\/I> the drive label for a drive? If it wasn\u2019t baseball season this would probably be our answer: \u201cNo, you can\u2019t. Now go away and leave us alone.\u201d But seeing as how this <I>is<\/I> baseball season our answer is a little different: \u201cOf <I>course<\/I> you can do this. And we\u2019d be more than happy to show you how.\u201d<\/P>\n<P>In fact, here\u2019s how.<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where DeviceID = &#8216;D:'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    objItem.VolumeName = &#8220;Data Drive&#8221;\n    objItem.Put_\nNext\n<\/PRE>\n<P>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:<\/P><PRE class=\"codeSample\">objItem.VolumeName = &#8220;Data Drive&#8221;\nobjItem.Put_\n<\/PRE>\n<P>What are we doing this time around? Well, this time around we\u2019re assigning a new drive label (in this case, <I>Data Drive<\/I>) to the VolumeName property; we can do that because VolumeName happens to be a read-write value. After that we simply call the <B>Put_<\/B> 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: <I>Data Drive<\/I>. It just doesn\u2019t get any better than that, does it?<\/P>\n<P>And now, if you\u2019ll excuse us, we have a \u2026 meeting \u2026 to go to. Right. A meeting \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Given the drive letter, how can I determine the drive label?&#8212; PM 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[216,3,12,5],"class_list":["post-67763","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-disk-drives-and-volumes","tag-scripting-guy","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Given the drive letter, how can I determine the drive label?&#8212; PM 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67763","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=67763"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67763\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=67763"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67763"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67763"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}