{"id":69753,"date":"2005-05-23T20:44:00","date_gmt":"2005-05-23T20:44:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/23\/how-can-i-correlate-logical-drives-and-physical-disks\/"},"modified":"2005-05-23T20:44:00","modified_gmt":"2005-05-23T20:44:00","slug":"how-can-i-correlate-logical-drives-and-physical-disks","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-correlate-logical-drives-and-physical-disks\/","title":{"rendered":"How Can I Correlate Logical Drives and Physical Disks?"},"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! How can I correlate logical drives and physical disks?<BR><BR>&#8212; LB<\/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, LB. We\u2019ll make a deal with you: we\u2019ll show you a script that can tell you which logical drives are found on which physical disks, and you promise not to say, \u201cWait, I don\u2019t understand how this script works.\u201d To tell you the truth, we\u2019re not so sure that <I>we<\/I> understand how the script works, either. As you\u2019ll see it\u2019s a bit wacky, to say the least.<\/P>\n<P>The problem is that there is no simple, straightforward way to retrieve the logical disks associated with a given physical disk. Instead, we have to use a couple of association classes (<B>Win32_DiskDriveToDiskPartition<\/B> and <B>Win32_LogicalDiskToPartition<\/B>) to figure out the relationship between logical drives and disks. There\u2019s nothing wrong with doing this, except that association classes can be extremely confusing, and the code required to use them can be even <I>more<\/I> confusing. If you\u2019re a relative newcomer to scripting, this might be a time when you just use the code without trying too terribly hard to understand it all. <\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colDiskDrives = objWMIService.ExecQuery(&#8220;SELECT * FROM Win32_DiskDrive&#8221;)<\/p>\n<p>For Each objDrive In colDiskDrives\n    Wscript.Echo &#8220;Physical Disk: &#8221; &amp; objDrive.Caption &amp; &#8221; &#8212; &#8221; &amp; objDrive.DeviceID \n    strDeviceID = Replace(objDrive.DeviceID, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\n    Set colPartitions = objWMIService.ExecQuery _\n        (&#8220;ASSOCIATORS OF {Win32_DiskDrive.DeviceID=&#8221;&#8221;&#8221; &amp; _\n            strDeviceID &amp; &#8220;&#8221;&#8221;} WHERE AssocClass = &#8221; &amp; _\n                &#8220;Win32_DiskDriveToDiskPartition&#8221;)<\/p>\n<p>    For Each objPartition In colPartitions\n        Wscript.Echo &#8220;Disk Partition: &#8221; &amp; objPartition.DeviceID\n        Set colLogicalDisks = objWMIService.ExecQuery _\n            (&#8220;ASSOCIATORS OF {Win32_DiskPartition.DeviceID=&#8221;&#8221;&#8221; &amp; _\n                objPartition.DeviceID &amp; &#8220;&#8221;&#8221;} WHERE AssocClass = &#8221; &amp; _\n                    &#8220;Win32_LogicalDiskToPartition&#8221;)<\/p>\n<p>        For Each objLogicalDisk In colLogicalDisks\n            Wscript.Echo &#8220;Logical Disk: &#8221; &amp; objLogicalDisk.DeviceID\n        Next\n        Wscript.Echo\n    Next\n    Wscript.Echo\nNext\n<\/PRE>\n<P>We told you it was weird-looking, didn\u2019t we? It actually starts off innocently enough: we simply connect to the WMI service and then use the <B>ExecQuery<\/B> method to retrieve a collection of all the physical disk drives installed on a computer. We create a For Each loop to loop through the collection of disk drives and echo the values of the <B>Caption<\/B> and <B>DeviceID<\/B> properties.<\/P>\n<P>Now it starts to get a little wild. The DeviceID &#8211; which uniquely identifies each disk drive &#8211; will have a value like this: <B>\\\\.\\PHYSICALDRIVE0<\/B>. We\u2019re going to use this value in a query, so we have to \u201cescape\u201d each \\ with a second \\. (Why? Because a single \\ is a reserved character in WMI.) That\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">strDeviceID = Replace(objDrive.DeviceID, &#8220;\\&#8221;, &#8220;\\\\&#8221;)\n<\/PRE>\n<P>After we do that we can then use this query to return all the disk partitions associated with the first physical disk drive on the computer:<\/P><PRE class=\"codeSample\">Set colPartitions = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_DiskDrive.DeviceID=&#8221;&#8221;&#8221; &amp; _\n        strDeviceID &amp; &#8220;&#8221;&#8221;} WHERE AssocClass = &#8221; &amp; _\n            &#8220;Win32_DiskDriveToDiskPartition&#8221;)\n<\/PRE>\n<P>Believe it or not we\u2019re making progress now; after all, we now have a collection of all the disk partitions found on the first drive. With the partitions in hand we set up a second For Each loop to iterate through these partitions; for each partition in the collection we then use this query to retrieve all of the logical drives found on those partitions:<\/P><PRE class=\"codeSample\">Set colLogicalDisks = objWMIService.ExecQuery _\n    (&#8220;ASSOCIATORS OF {Win32_DiskPartition.DeviceID=&#8221;&#8221;&#8221; &amp; _\n        objPartition.DeviceID &amp; &#8220;&#8221;&#8221;} WHERE AssocClass = &#8221; &amp; _\n            &#8220;Win32_LogicalDiskToPartition&#8221;)\n<\/PRE>\n<P>All that\u2019s left now is to set up a third For Each loop, this one to echo back all the logical disks found on the first partition of the first physical disk:<\/P><PRE class=\"codeSample\">For Each objLogicalDisk In colLogicalDisks\n    Wscript.Echo &#8220;Logical Disk: &#8221; &amp; objLogicalDisk.DeviceID\nNext\n<\/PRE>\n<P>We loop around and repeat the process for all the remaining partitions on disk 1, then loop back even further and repeat the process for any additional disk drives found on the computer. When you\u2019re all done, you\u2019ll get back a report similar to this:<\/P><PRE class=\"codeSample\">Physical Disk: IC25N040ATMR04-0 &#8212; \\\\.\\PHYSICALDRIVE0\nDisk Partition: Disk #0, Partition #0<\/p>\n<p>Disk Partition: Disk #0, Partition #1\nLogical Disk: C:<\/p>\n<p>Disk Parititon: Disk #0, Partition #2\nLogical Disk: D:<\/p>\n<p>Physical Disk: Memory Stick Slot &#8212; \\\\.\\PHYSICALDRIVE1\n<\/PRE>\n<P>As you can see, the first disk has three partitions and two logical drives: drives C and D. The second disk has no partitions or logical drives. (What, you think Microsoft would give the Scripting Guys computers that actually had two disk drives in them?)<\/P>\n<P>Like we said, if you don\u2019t follow all this, don\u2019t worry about it. No one knows how hot dogs are made, either, and the truth is, you probably don\u2019t <I>want<\/I> to know. Just slap some mustard and relish on the thing and enjoy. The same thing applies to this script (although possibly without the mustard and relish).<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I correlate logical drives and physical disks?&#8212; LB Hey, LB. We\u2019ll make a deal with you: we\u2019ll show you a script that can tell you which logical drives are found on which physical disks, and you promise not to say, \u201cWait, I don\u2019t understand how this script works.\u201d To tell [&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,6],"class_list":["post-69753","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","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I correlate logical drives and physical disks?&#8212; LB Hey, LB. We\u2019ll make a deal with you: we\u2019ll show you a script that can tell you which logical drives are found on which physical disks, and you promise not to say, \u201cWait, I don\u2019t understand how this script works.\u201d To tell [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69753","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=69753"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69753\/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=69753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}