{"id":66883,"date":"2006-07-18T07:54:00","date_gmt":"2006-07-18T07:54:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/07\/18\/how-can-i-create-a-logon-script-that-will-open-a-users-mapped-drives\/"},"modified":"2006-07-18T07:54:00","modified_gmt":"2006-07-18T07:54:00","slug":"how-can-i-create-a-logon-script-that-will-open-a-users-mapped-drives","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-logon-script-that-will-open-a-users-mapped-drives\/","title":{"rendered":"How Can I Create a Logon Script That Will Open a User\u2019s Mapped Drives?"},"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 create a logon script that will open a user\u2019s mapped drives?<BR><BR>&#8212; KZ<\/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, KZ. You know, a lot of people think we decided to write this column because we wanted yet another way to tell people about scripting. That\u2019s partially true; however, another reason there\u2019s a <I>Hey, Scripting Guy!<\/I> column is because the Scripting Guy who writes the column has a habit of stumbling across little bits of trivia that he finds absolutely fascinating. The problem with that? No one else ever seems to be as fascinated as he is. And that\u2019s <I>really<\/I> why he writes this column. In a face-to-face conversation people can sigh, roll their eyes, or spray him with Mace when he takes off on one of his boring tangents. In print, however, he can get away with boring people all he wants. (No; that\u2019s true; haven\u2019t you ever read anything by William Shakespeare?)<\/P>\n<P>With that in mind, you probably know that, on the night that the Titanic sank, the band heroically played on, trying to calm and comfort the doomed passengers as best they could. But here\u2019s something you might not have known. How did the White Star Line, owners of the Titanic, reward those musicians for their courage and their heroism? That\u2019s right: by billing their surviving family members for the lost band uniforms! True story.<\/P>\n<P>Did you hear that? It sounded like several thousand people sighing through the computer speakers. Weird.<\/P>\n<TABLE id=\"EBD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. In case you\u2019re wondering, no, Microsoft would never bill the Scripting Families for lost uniforms should the Scripting Guys be lost at sea. Granted we don\u2019t actually <I>wear<\/I> uniforms, but they wouldn\u2019t do that anyway.<\/P>\n<P>Probably.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As a matter of fact, KZ, we <I>do<\/I> have plenty more anecdotes we could relate to you. But, on the off chance that someone has figured out a way to Mace daily columnists via the Internet, maybe we should take a moment to show you a script that can open a user\u2019s mapped drives:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_MappedLogicalDisk&#8221;)<\/p>\n<p>Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>For Each objItem in colItems\n    strDrive = objItem.DeviceID\n    objShell.Run(strDrive)\nNext\n<\/PRE>\n<P>Let\u2019s begin at the beginning. The very first line in the script is <B>On Error Resume Next<\/B>. Typically we don\u2019t use any kind of error-handling in our sample scripts; that\u2019s because we want to keep our scripts as short and sweet as possible (just like Greg). In this case, however, we decided to make an exception. We don\u2019t know how it is in the rest of the world, but here at Microsoft networks occasionally go down, meaning that network resources (like mapped drives) aren\u2019t always available. By using On Error Resume Next we keep the script from crashing should one of the user\u2019s mapped drives not be available.<\/P>\n<P>Good question: how do we even know which of the user\u2019s drives are mapped drives? Let\u2019s put it this way: if you\u2019ve ever wondered what the WMI class <B>Win32_MappedLogicalDisk<\/B> is used for, well, now you know: this class returns a collection of all the mapped drives for the logged-on user. To get at this information all we have to do is connect to the WMI service on the local computer (because this is a logon script, we don\u2019t care about connecting to remote computers) and then use this line of code to retrieve the collection of mapped drives:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_MappedLogicalDisk&#8221;)\n<\/PRE>\n<P>Pretty easy, huh?<\/P>\n<P>Best of all, opening each of these drives is just as easy. What we\u2019re going to do is open the drives exactly the same way we would open these drives from the <B>Run<\/B> dialog box. For example, to open drive Z: from the Run dialog box we\u2019d simply type the following and then press ENTER:<\/P><PRE class=\"codeSample\">Z:\n<\/PRE>\n<P>We\u2019re going to do the exact same thing here, only programmatically.<\/P>\n<P>To do that, we first create an instance of the <B>Wscript.Shell<\/B> object; this is the scripting object that allows us to run executable files (like Explorer.exe) from within a script. We then set up a For Each loop that walks through the collection of mapped drives (we need to use a For Each loop regardless of whether the user has one mapped drive, 10 mapped drives, or even 0 mapped drives). Inside that loop we use this line of code to grab the <B>DeviceID<\/B> for each of these mapped drives; fortunately for us, the DeviceID just happens to be the drive letter for each mapped drive (e.g., Z:):<\/P><PRE class=\"codeSample\">strDrive = objItem.DeviceID\n<\/PRE>\n<P>Once we know the drive letter we can then use the <B>Run<\/B> method to open the mapped drive:<\/P><PRE class=\"codeSample\">objShell.Run(strDrive)\n<\/PRE>\n<P>That\u2019s all we have to do. We then loop around and repeat the process with any other mapped drives in the collection.<\/P>\n<P>Hmmm, that\u2019s odd: the White Star Line just sent us a bill for that script, even though we were the ones who wrote the script. <\/P>\n<P>Now here\u2019s a bonus script for you, KZ. Depending on your setup, you might not want to open <I>all<\/I> the mapped drives associated with a user; instead, you might only want to open that user\u2019s home drive (as configured in Active Directory). If that\u2019s the case, here\u2019s a script that will open just the home drive for the logged-on user:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>strUser = objSysInfo.UserName\nSet objUser = GetObject(&#8220;LDAP:\/\/&#8221; &amp; strUser)<\/p>\n<p>strDrive =  objUser.homeDrive\nobjShell.Run(strDrive)\n<\/PRE>\n<P>This time around we use the <B>ADSystemInfo<\/B> object, which provides basic information about the logged-on user and the computer he or she is logged on to. After we create the ADSystemInfo object we can then use this line of code to retrieve the user\u2019s distinguished name (e.g., <I>cn=Ken Myer, OU=Finance, dc=fabrikam, dc=com<\/I>), a name we can get from the <B>UserName<\/B> property:<\/P><PRE class=\"codeSample\">strUser = objSysInfo.UserName\n<\/PRE>\n<P>With the distinguished name in hand we can then use the <B>GetObject<\/B> method to bind to the user account in question. At that point we simply need to retrieve the value of the <B>homeDrive<\/B> property and then, again, use the <B>Run<\/B> method to open the drive:<\/P><PRE class=\"codeSample\">strDrive =  objUser.homeDrive\nobjShell.Run(strDrive)\n<\/PRE>\n<P>And there you have it, KZ. Now, back to the amusing anecdotes. Although everyone has heard of Genghis Khan, most people aren\u2019t aware that &#8211;<\/P>\n<P>Oh; OK. Sorry, folks, but the White Star Line has said we\u2019ve used up our quota of Web space for today. We\u2019re afraid we won\u2019t be able to tell you any more interesting little stories, at least not for now.<\/P>\n<TABLE id=\"E1F\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Wow, a standing ovation; we didn\u2019t know you could do <I>that<\/I> over the Internet. Thanks, guys! (Even if it <I>was<\/I> for the White Star Line and not us.)<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a logon script that will open a user\u2019s mapped drives?&#8212; KZ Hey, KZ. You know, a lot of people think we decided to write this column because we wanted yet another way to tell people about scripting. That\u2019s partially true; however, another reason there\u2019s a Hey, Scripting Guy! [&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-66883","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! How can I create a logon script that will open a user\u2019s mapped drives?&#8212; KZ Hey, KZ. You know, a lot of people think we decided to write this column because we wanted yet another way to tell people about scripting. That\u2019s partially true; however, another reason there\u2019s a Hey, Scripting Guy! [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66883","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=66883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66883\/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=66883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}