{"id":68863,"date":"2005-09-28T16:34:00","date_gmt":"2005-09-28T16:34:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/28\/how-can-i-find-all-the-drives-mapped-to-a-share-and-remap-them\/"},"modified":"2005-09-28T16:34:00","modified_gmt":"2005-09-28T16:34:00","slug":"how-can-i-find-all-the-drives-mapped-to-a-share-and-remap-them","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-find-all-the-drives-mapped-to-a-share-and-remap-them\/","title":{"rendered":"How Can I Find All the Drives Mapped to a Share and Remap Them?"},"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 find all the drives mapped to \\\\server1\\share and remap them to \\\\server2\\share? <BR><BR>&#8212; H T-S<\/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, H T-S. You know, not too long ago Malcolm Gladwell published a book called <I>The Tipping Point<\/I>. Loosely speaking, the book posited that something could be ignored for the longest time, at least until it reached the so-called tipping point; upon doing so, this thing that nobody cared about would suddenly became a veritable craze. Seemingly overnight it would go from being something you never heard of to something that seems to show up everywhere you look.<\/P>\n<P>It\u2019s an interesting conjecture, and we seem to be seeing this phenomenon when it comes to scripts that map and unmap network drives. For over a year we published the <I>Hey, Scripting Guy!<\/I> column without mentioning network drives, and nobody seemed to notice. And then, suddenly, we were <I>deluged<\/I> with questions about mapping and unmapping network drives. We answered the first such question <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/sept05\/hey0915.mspx\"><B>a couple weeks ago<\/B><\/A>; now we\u2019re back to answer another one, and we have an Inbox full of additional questions about network drives. First hula hoops, then bell-bottom pants, now network drives. Go figure.<\/P>\n<P>So what <I>about<\/I> remapping network drives? Well, for better or worse there\u2019s no RemapNetworkDrive method that can take care of thus automatically; therefore, we have to come up with a minor workaround. But that\u2019s not too bad: we can carry out this task by locating any drives that meet the criteria, unmapping those drives, then remapping each drive to the new location. <\/P>\n<P>Sure, it <I>sounds<\/I> complicated, but it\u2019s really pretty easy. Here\u2019s a script that looks for any drive mapped to <B>\\\\server1\\share<\/B> and remaps it to <B>\\\\server2\\share<\/B>:<\/P><PRE class=\"codeSample\">Set objNetwork = CreateObject(&#8220;Wscript.Network&#8221;)<\/p>\n<p>Set colDrives = objNetwork.EnumNetworkDrives<\/p>\n<p>For i = 0 to colDrives.Count-1 Step 2\n    If colDrives.Item(i + 1) = &#8220;\\\\server1\\share&#8221; Then\n        strDriveLetter = colDrives.Item(i)\n        objNetwork.RemoveNetworkDrive strDriveLetter\n        objNetwork.MapNetworkDrive strDriveLetter, &#8220;\\\\server2\\share&#8221;\n    End If\nNext\n<\/PRE>\n<P>The script starts off by creating an instance of the <B>Wscript.Network<\/B> object. We should note that we need to use Windows Script Host any time we want to map or unmap network drives; that\u2019s because WMI doesn\u2019t have any methods for mapping or unmapping drives. That\u2019s fine, except it means that our script must be run on the local computer: WSH methods typically can\u2019t be used against remote machines. That\u2019s a limitation you\u2019ll just have to deal with. (One way to deal with it: run the script as a logon script. Logon scripts always run locally.)<\/P>\n<P>After creating the Network object we then call the <B>EnumNetworkDrives<\/B> method in order to return a collection of all the mapped network drives on the computer:<\/P><PRE class=\"codeSample\">Set colDrives = objNetwork.EnumNetworkDrives\n<\/PRE>\n<P>That brings us face-to-face with that curious little animal known as the mapped network drives collection. We won\u2019t detail the architecture of this collection today; for that, see our <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/sept05\/hey0915.mspx\"><B>previous column<\/B><\/A> on network drives. Suffice to say that each mapped drive actually takes up <I>two<\/I> items in the collection: the first item for the drive letter, the second for the UNC path. If we have three mapped drives on a computer our collection will look something like this:<\/P><PRE class=\"codeSample\">X:\n\\\\server1\\share1\nY:\n\\\\server2\\share2\nZ:\n\\\\server3\\share3\n<\/PRE>\n<P>That\u2019s why we have to use such a crazy-looking For Next loop in order to walk through the collection; this line of code causes us to skip every other item in the collection, in turn ensuring that we land only on the drive letters:<\/P><PRE class=\"codeSample\">For i = 0 to colDrives.Count-1 Step 2\n<\/PRE>\n<P>For each drive letter we then need to determine whether the corresponding UNC path is equal to \\\\server1\\share1. Remember, if we\u2019re looking at item 0 in the collection (the first item in the collection has an index number of 0) then we\u2019re looking at a drive letter; the corresponding UNC path will be that index number (0) plus 1. Therefore, we use code like this to determine whether or not the first drive happens to be mapped to \\\\server1\\share1:<\/P><PRE class=\"codeSample\">If colDrives.Item(i + 1) = &#8220;\\\\server1\\share&#8221; Then\n<\/PRE>\n<P>Let\u2019s assume that it is. In that case, we need to grab the drive letter (item 0) and store that value in a variable named strDriveLetter. We then call the <B>RemoveNetworkDrive<\/B> method to unmap the drive, then call the <B>MapNetworkDrive<\/B> method to remap that same drive letter to the new share:<\/P><PRE class=\"codeSample\">objNetwork.MapNetworkDrive strDriveLetter, &#8220;\\\\server2\\share&#8221;\n<\/PRE>\n<P>And no, they don\u2019t call it the \u201ctipping point\u201d because you just tipped over trying to follow all this. We know it\u2019s a bit confusing, but that\u2019s because of the strange way in which the mapped network drives collection is constructed. If this doesn\u2019t make any sense to you, try drawing it out in a flowchart of some kind; you should see that there <I>is<\/I> a logic to the whole thing. A somewhat twisted logic, perhaps, but a logic nonetheless.<\/P>\n<P>Because this is confusing we went with the simplest possible example: a share named \\\\server1\\share is remapped to a share named \\\\server2\\share. It\u2019s also possible to remap <I>any<\/I> share on server1 to any similarly-named share on server2. We thought that was a bit much for today, however. If that\u2019s of interest to you, though, just let us know, and we\u2019ll revisit the topic in the near future.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I find all the drives mapped to \\\\server1\\share and remap them to \\\\server2\\share? &#8212; H T-S Hey, H T-S. You know, not too long ago Malcolm Gladwell published a book called The Tipping Point. Loosely speaking, the book posited that something could be ignored for the longest time, at least [&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-68863","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 find all the drives mapped to \\\\server1\\share and remap them to \\\\server2\\share? &#8212; H T-S Hey, H T-S. You know, not too long ago Malcolm Gladwell published a book called The Tipping Point. Loosely speaking, the book posited that something could be ignored for the longest time, at least [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68863","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=68863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68863\/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=68863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}