{"id":68953,"date":"2005-09-15T14:20:00","date_gmt":"2005-09-15T14:20:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/09\/15\/how-can-i-use-a-logon-script-to-remove-all-mapped-drives\/"},"modified":"2005-09-15T14:20:00","modified_gmt":"2005-09-15T14:20:00","slug":"how-can-i-use-a-logon-script-to-remove-all-mapped-drives","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-a-logon-script-to-remove-all-mapped-drives\/","title":{"rendered":"How Can I Use a Logon Script to Remove All Mapped Drives?"},"content":{"rendered":"<p><P><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> <\/P>\n<P>Hey, Scripting Guy! How can I use a logon script to remove all mapped drives each time a user logs on?<BR><BR>&#8212; VS<\/P>\n<P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> <\/P>\n<P>Hey, VS. You know, we\u2019re glad you specifically mentioned the term \u201clogon script\u201d in your question. Why? Well, one of the drawbacks to mapping and unmapping network drives is the fact that there are no WMI classes capable of performing these tasks; that means we have to rely on Windows Script Host to remove mapped network drives. WSH does the job just fine, but with one caveat: WSH can map and unmap drives only on the local computer. That can be a bit inconvenient, to say the least (there\u2019s no straightforward way to run a script that unmaps drives on a remote machine), but in this case it doesn\u2019t matter; that\u2019s because logon scripts always run locally anyway.<\/P>\n<P>So how do you remove all the mapped network drives on a computer? Here\u2019s one way:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>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    objNetwork.RemoveNetworkDrive colDrives.Item(i)\nNext\n<\/PRE>\n<P>Yes, this <I>is<\/I> odd-looking code, but we\u2019ll explain why it looks that way. First, though, let\u2019s point out that the script starts off by creating an instance of the <B>Wscript.Network<\/B> object; we then call the <B>EnumNetworkDrives<\/B> method to return a collection of all the mapped network drives on a computer.<\/P>\n<P>This is where the quirkiness of WSH\u2019s mapped network drives collection manifests itself. Suppose you have three mapped drives on your computer (drives X, Y, and Z). The collection returned by EnumNetworkDrives will look something like this:<\/P><PRE class=\"codeSample\">X:\n\\\\atl-fs-01\\share1\nY:\n\\\\atl-fs-01\\share2\nZ:\n\\\\atl-fs-01\\share3\n<\/PRE>\n<P>As you can see, each mapped drive actually has two entries in the collection. Take drive X, for example. The first item in the collection (item 0) is the drive letter for drive X: X:. The second item in the collection (item 1) is the UNC path to the shared folder on drive X: \\\\atl-fs-01\\share1. One drive, but two entries in the mapped network drive collection. Each of the other drives also has two items in the collection, meaning our three mapped drives will result in a six-item collection, with items alternating between drive letter and UNC path.<\/P>\n<P>That\u2019s why we can\u2019t use a simple For Each loop to loop through the items in the collection. In order to remove a network drive, we need to call the <B>RemoveNetworkDrive<\/B> method, passing the method the drive letter for the drive to be removed. If we looped through all the items in the collection, the first time through the loop we\u2019d pass RemoveNetworkDrive the value <B>X:<\/B>. So far so good; we\u2019d remove drive X. However, the second time through the loop we\u2019d pass RemoveNetworkDrive the value <B>\\\\atl-fs-01-share1<\/B>. That\u2019s not so good: there aren\u2019t going to be any drives with a drive letter \\\\atl-fs-01-share1. Which, of course, means that you\u2019re script is going to fail.<\/P>\n<P>Which also means we need to figure out a way around this issue. To that end, we use a For Next loop, looping from 0 (the first item in a collection is always item 0) to the number of drives in the collection minus 1. (Why \u201cminus 1? Well, in a three-item collection item numbers would be 0, 1, and 2. Because the first item is item 0, the last item will always be the total number of items minus 1; in this case, 3-1, or 2. If you have 100 items in the collection the last item will have an index number of 99, or 100-1.)<\/P>\n<P>On top of that, we also need to skip every other item; instead of hitting items 0, 1, 2, 3, 4, 5, we want to hit only items 0, 2, and 4 (the items that contain drive letters). That\u2019s what the <B>Step 2<\/B> does; it tells VBScript to only hit every other item. (Step 1, the default value for a For Next loop, hits every item.) Setting the Step value to 2 ensures that we\u2019ll pass only drive letters to the RemoveNetworkDrive method.<\/P>\n<P>Now that we have a way of grabbing just the drive letters (bypassing the UNC paths), the rest is easy: to remove a network drive we simply use this line of code, with colDrives.Item(i) representing the drive letter for the current item in the collection:<\/P><PRE class=\"codeSample\">objNetwork.RemoveNetworkDrive colDrives.Item(i)\n<\/PRE>\n<P>With any luck (although we\u2019re talking computers here; who needs luck when working with computers?) any and all of the mapped drives found on this machine will be removed.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use a logon script to remove all mapped drives each time a user logs on?&#8212; VS Hey, VS. You know, we\u2019re glad you specifically mentioned the term \u201clogon script\u201d in your question. Why? Well, one of the drawbacks to mapping and unmapping network drives is the fact that there [&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-68953","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 use a logon script to remove all mapped drives each time a user logs on?&#8212; VS Hey, VS. You know, we\u2019re glad you specifically mentioned the term \u201clogon script\u201d in your question. Why? Well, one of the drawbacks to mapping and unmapping network drives is the fact that there [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68953","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=68953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68953\/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=68953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}