{"id":68023,"date":"2006-02-06T18:50:00","date_gmt":"2006-02-06T18:50:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/06\/how-can-i-assign-a-new-home-drive-to-a-user-who-doesnt-have-one\/"},"modified":"2006-02-06T18:50:00","modified_gmt":"2006-02-06T18:50:00","slug":"how-can-i-assign-a-new-home-drive-to-a-user-who-doesnt-have-one","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-assign-a-new-home-drive-to-a-user-who-doesnt-have-one\/","title":{"rendered":"How Can I Assign a New Home Drive to a User Who Doesn\u2019t Have One?"},"content":{"rendered":"<p><P>&nbsp;<\/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 check to see if a user has a home drive and home directory set and, if they don\u2019t, assign them a new one?<BR><BR>&#8212; AM<\/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, AM. No, no excuses: the Seahawks had plenty of opportunities to win the Super Bowl yesterday, but they failed to take advantage of it. At times it seemed like they were playing not to lose instead of playing to win, and that\u2019s always a recipe for disaster. Oh, well; that\u2019s the way the world goes round.<\/P>\n<P>Of course, considering how far they had to travel to get there you\u2019d think that the officials would have been interested in watching the game themselves. But apparently not. Matt Hasselbeck gets called for blocking below the waist while making a tackle? OK \u2026.<\/P>\n<P>No, that\u2019s not an excuse: it\u2019s just an \u2026 observation \u2026.<\/P>\n<P>But, hey, what\u2019s done is done, and, besides, this is supposed to be a <I>scripting<\/I> column, not a sports column. With that in mind, how <I>can<\/I> we test for the existence of a home directory and, if necessary, assign a new home directory and home drive for the user? Well, this should do the trick:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objUser = GetObject(&#8220;LDAP:\/\/cn=Ken Myer, ou=Finance, dc=fabrikam, dc=com&#8221;)<\/p>\n<p>If IsEmpty(objUser.homeDirectory) or IsNull(objUser.homeDirectory) Then\n    strUser = objUser.sAMAccountName\n    strHomeDirectory = &#8220;\\\\atl-dc-01\\users\\&#8221; &amp; strUser\n    objUser.homeDirectory = strHomeDirectory\n    objUser.homeDrive = &#8220;Z:&#8221;\n    objUser.SetInfo\nEnd If\n<\/PRE>\n<P>In this script we begin by binding to the Ken Myer user account in Active Directory. That brings us to this line of code:<\/P><PRE class=\"codeSample\">If IsEmpty(objUser.homeDirectory) or IsNull(objUser.homeDirectory) Then\n<\/PRE>\n<P>As you probably know, Active Directory scripting can be a tad bit tricky from time-to-time. Suppose you look at a user account in Active Directory Users and Computers and see that the home directory box is blank. In that case, your first thought is likely to be, \u201cOh, I guess this user doesn\u2019t have a home directory.\u201d And you would be right.<\/P>\n<P>If you run a script against this same account, however, you have to concern yourself with <I>two<\/I> things: is the home directory box blank because the attribute is null (that is, no value has been assigned to it), or is the home directory box blank because the attribute is empty (perhaps because another administrator ran a script that set the value to an empty string)? <\/P>\n<P>Yes, we know: an empty attribute and a Null attribute would seem to be the same thing. For better or worse, though, they aren\u2019t. Technically speaking, an empty attribute actually has a value: it\u2019s just that the value is nothing. With a Null attribute, however, we\u2019re saying that we have no idea whether or not this property has a value. It\u2019s confusing, but it\u2019s an important distinction. If the homeDirectory attribute is empty that means that the user doesn\u2019t have a home directory assigned. However, in that case the following test will return False and lead us to believe that the user <I>does<\/I> have a home directory:<\/P><PRE class=\"codeSample\">IsNull(objUser.homeDirectory)\n<\/PRE>\n<P>Why does this return False? Well, because the attribute isn\u2019t Null, it\u2019s empty. And, like it or not, there\u2019s a difference<\/P>\n<P>That\u2019s all right: let us know when your head stops spinning and then we\u2019ll continue.<\/P>\n<P>Better? To be honest, this isn\u2019t worth worrying about, at least not right now. Instead, just play it safe and do what we did: check to see if the attribute value is empty <I>or<\/I> if it\u2019s Null. You can do that by using both the <B>IsEmpty<\/B> and <B>IsNull<\/B> functions in your If-Then statement:<\/P><PRE class=\"codeSample\">If IsEmpty(objUser.homeDirectory) or IsNull(objUser.homeDirectory) Then\n<\/PRE>\n<P>This way you\u2019re covered no matter what. <\/P>\n<P>So suppose the user doesn\u2019t have a home directory; what then? Well, then we execute this block of code:<\/P><PRE class=\"codeSample\">strUser = objUser.sAMAccountName\nstrHomeDirectory = &#8220;\\\\atl-dc-01\\users\\&#8221; &amp; strUser\nobjUser.homeDirectory = strHomeDirectory\nobjUser.homeDrive = &#8220;Z:&#8221;\nobjUser.SetInfo\n<\/PRE>\n<P>Home directories are typically named after the user. (They don\u2019t have to be, but that\u2019s the standard adopted in many organizations.) Because we\u2019re too lazy to come up with our standard, we\u2019re going to use this same approach; that means we\u2019re going to assign the user the home directory <B>\\\\atl-dc-01\\users\\<\/B><B><I>logonname<\/I><\/B>, where <I>logonname<\/I> represents the user\u2019s logon name (sAMAccountName). Granted, we could hard-code this name into the script. However, we decided to get a tiny bit fancy and have the script create the home directory name for us. <\/P>\n<P>To do that we first assign the value of the user\u2019s sAMAccountName (for example, kenmyer) to a variable named strUser. We then use this line of code to construct the UNC path to the directory:<\/P><PRE class=\"codeSample\">strHomeDirectory = &#8220;\\\\atl-dc-01\\users\\&#8221; &amp; strUser\n<\/PRE>\n<P>If the user\u2019s sAMAccountName really <I>is<\/I> kenmyer then the resulting path (and the value assigned to the variable strHomeDirectory) will be this:<\/P><PRE class=\"codeSample\">\\\\atl-dc-01\\users\\kenmyer\n<\/PRE>\n<P>Once we have the complete path we can set the value of the homeDirectory attribute; after that we\u2018re free to set the value of the homeDrive attribute to Z:. (And, yes, you must include the colon following the drive letter.) And when all <I>that\u2019s<\/I> done we can call <B>SetInfo<\/B> to write the changes back to the user account. Whatever you do, don\u2019t leave out the call to SetInfo: if you do your changes will <I>not<\/I> be applied to Active Directory. Which in turn means the user <I>still<\/I> won\u2019t have a home drive and directory. <\/P>\n<P>So are we really putting away the sports references forever? Of course not: after all, the end of the Super Bowl simply means that baseball season is just around the corner. And this year we think that our very own Seattle Mariners are going to &#8211;<\/P>\n<P>On second thought, we\u2019ll get back to you about those sports references \u2026.<\/P><BR>\n<DIV>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\" width=\"100%\">\n<TBODY>\n<TR>\n<TD><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb06\/hey0206.mspx\/#top\"><IMG border=\"0\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/technet\/mnplibrary\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" height=\"9\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb06\/hey0206.mspx\/#top\">Top of page<\/A><\/TD><\/TR><\/TBODY><\/TABLE><\/DIV><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Hey, Scripting Guy! How can I check to see if a user has a home drive and home directory set and, if they don\u2019t, assign them a new one?&#8212; AM Hey, AM. No, no excuses: the Seahawks had plenty of opportunities to win the Super Bowl yesterday, but they failed to take advantage of [&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":[7,3,20,5],"class_list":["post-68023","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>&nbsp; Hey, Scripting Guy! How can I check to see if a user has a home drive and home directory set and, if they don\u2019t, assign them a new one?&#8212; AM Hey, AM. No, no excuses: the Seahawks had plenty of opportunities to win the Super Bowl yesterday, but they failed to take advantage of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68023","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=68023"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68023\/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=68023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68023"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}