{"id":68653,"date":"2005-10-27T14:26:00","date_gmt":"2005-10-27T14:26:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/10\/27\/how-can-i-determine-which-drives-are-mapped-to-network-shares\/"},"modified":"2005-10-27T14:26:00","modified_gmt":"2005-10-27T14:26:00","slug":"how-can-i-determine-which-drives-are-mapped-to-network-shares","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-which-drives-are-mapped-to-network-shares\/","title":{"rendered":"How Can I Determine Which Drives are Mapped to Network Shares?"},"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 determine which drives on a computer are mapped to network shares?<BR><BR>&#8212; CP<\/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, CP. OK, you better sit down, because what we\u2019re about to say will shock you: if you want to determine which drives on a computer are mapped to network shares, don\u2019t use the Win32_MappedLogicalDisk class.<\/P>\n<P>Oh, look: you spilled coffee on everything. Didn\u2019t we tell you to sit down? Here, here\u2019s some paper towels.<\/P>\n<P>While you clean up &#8211; don\u2019t forget that little puddle by the phone &#8211; we\u2019ll explain why. The truth is, you <I>can<\/I> use the WMI class Win32_MappedLogicalDisk to determine which drives are mapped to network shares. The problem, however, is that while this class will return the drive letters for these drives, it won\u2019t tell you which shares the drives are mapped to. Weird, but true.<\/P>\n<P>And so it\u2019s Win32_LogicalDisk to the rescue. With this class not only can we determine which drives are mapped to network shares, but we can also determine <I>which<\/I> network shares they map to. And to do that requires nothing more than a script like this:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colDrives = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where DriveType = 4&#8221;)<\/p>\n<p>For Each objDrive in colDrives\n    Wscript.Echo &#8220;Drive letter: &#8221; &amp; objDrive.DeviceID\n    Wscript.Echo &#8220;Network path: &#8221; &amp; objDrive.ProviderName\nNext\n<\/PRE>\n<P>Stop us if you\u2019ve heard this one before, but the script starts out by connecting to the WMI service on the local computer. (As usual, you can modify this script to run against a remote computer simply by assigning the name of that machine to the variable strComputer.) We then use this line of code to return a collection of all the mapped network drives:<\/P><PRE class=\"codeSample\">Set colDrives = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_LogicalDisk Where DriveType = 4&#8221;)\n<\/PRE>\n<P>The key here &#8211; as you might have guessed &#8211; lies in our Where clause. We\u2019re asking for all instances of the class where the <B>DriveType<\/B> is equal to 4; needless to say, a DriveType equal to 4 represents a mapped network drive. (For other DriveType values, see the <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/wmisdk\/wmi\/win32_logicaldisk.asp\" target=\"_blank\"><B>WMI SDK<\/B><\/A> on MSDN.) The query returns a collection of all the mapped drives; we then set up a For Each loop to walk through that collection. For each mapped drive we echo the value of two properties: <B>DeviceID<\/B>, which returns the drive letter for the drive; and <B>ProviderName<\/B>, which returns the network share the drive is mapped to. <\/P>\n<P>In other words, we get back information similar to this:<\/P><PRE class=\"codeSample\">Drive letter: E:\nNetwork path: \\\\atl-fs-01\\public\nDrive letter: F:\nNetwork path: \\\\atl-fs-01\\finance\nDrive letter: G:\nNetwork path: \\\\atl-fs-01\\users\\kenmyer\n<\/PRE>\n<P>That\u2019s OK, we understand: as useful as this script is, it still comes as a bit of a shock that we didn\u2019t use the Win32_MappedLogicalDisk class. Listen, this has been kind if rough on you: why don\u2019t you take the rest of the day off? Just tell your boss the Scripting Guys said it was OK.<\/P>\n<TABLE id=\"E1D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Legal disclaimer<\/B>. On the advice of our attorneys (and, trust us, the Scripting Guys <I>need<\/I> their own attorneys), we did <I>not<\/I> say it was OK. (But if you want to try it anyway, well, good luck.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine which drives on a computer are mapped to network shares?&#8212; CP Hey, CP. OK, you better sit down, because what we\u2019re about to say will shock you: if you want to determine which drives on a computer are mapped to network shares, don\u2019t use the Win32_MappedLogicalDisk class. Oh, [&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":[3,185,12,5],"class_list":["post-68653","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-shared-folders-and-mapped-drives","tag-storage","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine which drives on a computer are mapped to network shares?&#8212; CP Hey, CP. OK, you better sit down, because what we\u2019re about to say will shock you: if you want to determine which drives on a computer are mapped to network shares, don\u2019t use the Win32_MappedLogicalDisk class. Oh, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68653","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=68653"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68653\/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=68653"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68653"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68653"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}