{"id":64583,"date":"2007-06-26T00:13:00","date_gmt":"2007-06-26T00:13:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/06\/26\/how-can-i-create-a-folder-for-each-user-in-my-domain\/"},"modified":"2007-06-26T00:13:00","modified_gmt":"2007-06-26T00:13:00","slug":"how-can-i-create-a-folder-for-each-user-in-my-domain","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-a-folder-for-each-user-in-my-domain\/","title":{"rendered":"How Can I Create a Folder for Each User in My Domain?"},"content":{"rendered":"<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\"> \n<P>Hey, Scripting Guy! How can I create a folder for each user in my domain? I\u2019d like the folder name to consist of the user\u2019s first initial and their last name (for example, KMyer).<BR><BR>&#8212; GN <\/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> \n<P>Hey, GN. You know, this has been an interesting week for the Scripting Guys. Why? Because absolutely nothing interesting has happened to any of the Scripting Guys this entire week. Well, OK, <I>maybe<\/I> Scripting Guy Dean Tsaltas has had an interesting week. But Dean is currently off in the wilds of Nova Scotia, and no one has heard from him in the past few weeks. If his week is going anything like <I>our<\/I> week, however, he\u2019s no doubt sitting in the wilds of Nova Scotia thinking, \u201cMan, this is boring. I wonder if anything interesting has happened to the other Scripting Guys. And why exactly <I>am<\/I> I sitting in the wilds of Nova Scotia?\u201d<\/P>\n<P>Fortunately, today is Friday (although you don\u2019t actually get to read this particular column until the following Monday), and no doubt all sorts of exciting things will happen over the weekend to help snap the Scripting Guys out of their lethargy. For example, the Scripting Guy who writes this column is going to pull weeds; <I>that<\/I> should get the old juices flowing again! Scripting Guy Jean Ross is seriously considering packing a few things into boxes, although she\u2019s afraid that this might be too much excitement for a single weekend. (Maybe this weekend she\u2019ll get the boxes out, then save the actual packing for next week.) As for Scripting Guy Peter Costantini \u2013 well, to tell you the truth, the other Scripting Guys never ask too many questions about what Peter does over the weekend. There are some things we\u2019re just better off not knowing about.<\/P>\n<P>Of course, the bottom line is this: we have no amusing anecdotes or witty repartee we can use to spice up today\u2019s column. The truth is, we have nothing at all for today\u2019s column. Nothing.<\/P>\n<P>Well, except a script that can create a folder for each user in your domain:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const ADS_SCOPE_SUBTREE = 2<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objCommand = CreateObject(&#8220;ADODB.Command&#8221;)\nobjConnection.Provider = &#8220;ADsDSOObject&#8221;\nobjConnection.Open &#8220;Active Directory Provider&#8221;\nSet objCommand.ActiveConnection = objConnection<\/p>\n<p>objCommand.Properties(&#8220;Page Size&#8221;) = 1000\nobjCommand.Properties(&#8220;Searchscope&#8221;) = ADS_SCOPE_SUBTREE <\/p>\n<p>objCommand.CommandText = _\n    &#8220;SELECT givenName, sn FROM &#8216;LDAP:\/\/DC=fabrikam,DC=com&#8217; WHERE objectCategory=&#8217;user'&#8221; <\/p>\n<p>Set objRecordSet = objCommand.Execute<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)<\/p>\n<p>objRecordSet.MoveFirst<\/p>\n<p>Do Until objRecordSet.EOF\n    strInitial = Left(objRecordSet.Fields(&#8220;givenName&#8221;).Value, 1)\n    strFolderName = &#8220;C:\\Public\\&#8221; &amp; strInitial &amp; objRecordSet.Fields(&#8220;sn&#8221;).Value\n    Set objFolder = objFSO.CreateFolder(strFolderName)\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>As usual, we aren\u2019t going to spend much time discussing the ins and outs of Active Directory search scripts: we\u2019re far too lazy \u2013 uh, we simply don\u2019t have the space in the daily column to do that. But have no fear; after all, we still have a two-part <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/tales\/sg0405.mspx\"><B>Tales from the Script<\/B><\/A> series that tells you everything you need to know about searching Active Directory. And you are welcome to read that any time you want.<\/P>\n<TABLE class=\"dataTable\" id=\"EVD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Yes, that includes explaining everything from the Connection object to the Page Size property to the Searchscope property. We Scripting Guys may be boring, but at least we\u2019re thorough.<\/P>\n<P>Heck we might as well be thorough: we don\u2019t have anything else to do.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>About all we <I>will<\/I> do with the search portion of our script is take a quick peek at the SQL query that returns a collection of user information:<\/P><PRE class=\"codeSample\">objCommand.CommandText = _\n    &#8220;SELECT givenName, sn FROM &#8216;LDAP:\/\/DC=fabrikam,DC=com&#8217; WHERE objectCategory=&#8217;user'&#8221;\n<\/PRE>\n<P>As you can see, all we\u2019re doing here is selecting two attributes (<B>givenName<\/B> and <B>sn<\/B>) for all the objects in the fabrikam.com domain that meet a specific criterion. (Criterion, of course, being the singular form of criteria. Apparently we Scripting Guys aren\u2019t just thorough, we\u2019re also grammatical.) And just what <I>is<\/I> that criterion? It\u2019s this: returned objects must have an <B>objectCategory<\/B> equal to <I>user<\/I>. That\u2019s how we can return a collection of all the user accounts in our domain. (Well, along with the fact that we started our search in the domain root.) What if we wanted to return a list of all our <I>computer<\/I> accounts? Then we\u2019d use the following WHERE clause:<\/P><PRE class=\"codeSample\">WHERE objectCategory=&#8217;computer&#8217;\n<\/PRE>\n<P>And so on.<\/P>\n<P>And why givenName and sn? That\u2019s easy: those are the two Active Directory attributes that correspond to, respectively, the user\u2019s first name and last name. Wouldn\u2019t firstName have been a better attribute name than givenName? Probably. But, like it or not, we\u2019re stuck with givenName and sn.<\/P>\n<TABLE class=\"dataTable\" id=\"E5E\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. OK, so then what <I>other<\/I> user account attributes are available from Active Directory? Hard as this might be to believe, we don\u2019t know, at least not off the tops of our heads. But that\u2019s OK, because we can always look this information up in the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_usr_overview.mspx\" target=\"_blank\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A> or in the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/aa772170.aspx\" target=\"_blank\"><B>ADSI SDK<\/B><\/A>. And so can you.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After setting up our query we then call the <B>Execute<\/B> method to invoke the command and return a recordset consisting of all the user accounts in the domain fabrikam.com. <\/P>\n<TABLE class=\"dataTable\" id=\"E4F\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>True Microsoft story<\/B>. Although the method really <I>is<\/I> named <I>Execute<\/I> Microsoft editors always try to change the name; they\u2019re afraid that readers will think Microsoft is ordering up firing squads and other forms of execution. Which is silly; despite the name, no one from Microsoft is ordering executions. You have our word on that.<\/P>\n<P>Well, OK, technically the Scripting Guys have <I>ordered<\/I> executions; however, we\u2019ve never been able to find any one to carry out these executions for us. And we\u2019re too chicken to do them ourselves.<\/P>\n<P>Fortunately for us, the Scripting Editor isn\u2019t really a <I>Microsoft<\/I> editor; she\u2019s a <I>good <\/I>editor instead. Among other things, that means she\u2019ll let us use the word <I>execute<\/I>, although she won\u2019t actually execute anyone, either. Which, come to think of it, is probably fortunate for us as well \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>OK, where were we? Oh, right. After calling the <I>Execute<\/I> method (heh-heh; take <I>that<\/I> Microsoft editors!) we get back a recordset consisting of the first and last names of all the users in our domain. Because of that, our next step is to set up a Do Until loop that enables us to loop through the entire recordset (or, to make it sound as though we know what we\u2019re talking about, until the recordset\u2019s <B>EOF<\/B> \u2013 end-of-file \u2013 property is True):<\/P><PRE class=\"codeSample\">Do Until objRecordSet.EOF\n<\/PRE>\n<P>Oh, right: before we did that we also created an instance of the <B>Scripting.FileSystemObject<\/B>, the object needed to create our folders. Our apologies to Scripting.FileSystemObject for neglecting to mention this. <\/P>\n<P>Inside the Do Until loop we use this line of code to grab the first letter of the user\u2019s first name (i.e., their first initial):<\/P><PRE class=\"codeSample\">strInitial = Left(objRecordSet.Fields(&#8220;givenName&#8221;).Value, 1)\n<\/PRE>\n<P>As you can see, that\u2019s pretty straightforward: we just use the <B>Left<\/B> function to grab the first character in the user\u2019s givenName. And yes, the syntax for referring to recordset fields is a bit cumbersome: <B>objRecordSet.Fields(&#8220;givenName&#8221;).Value<\/B>. But don\u2019t worry too much about that: just copy and paste the string and leave it at that.<\/P>\n<P>Once we have the user\u2019s first initial we can then construct the name of our first folder:<\/P><PRE class=\"codeSample\">strFolderName = &#8220;C:\\Public\\&#8221; &amp; strInitial &amp; objRecordSet.Fields(&#8220;sn&#8221;).Value\n<\/PRE>\n<P>Here we\u2019re simply assigning a value to a variable named strFolderName. And what <I>is<\/I> that value? Well, assuming that the first user in our recordset is named Ken Myer that value will be <B>C:\\Public\\KMyer<\/B>. In other words, <\/P>\n<P><B>C:\\Public\\<\/B> plus <B>K<\/B> (first initial of first name) plus<B> Myer<\/B> (sn, or last name) = <B>C:\\Public\\KMyer<\/B><\/P>\n<P>At that point we call the <B>CreateFolder<\/B> method to create a new folder, passing the variable strFolderName as the sole method parameter:<\/P><PRE class=\"codeSample\">Set objFolder = objFSO.CreateFolder(strFolderName)\n<\/PRE>\n<P>From there we call the <B>MoveNext<\/B> method to move on to the next record in the recordset. Etc., etc.<\/P>\n<P>You know, that\u2019s a good question: if this has been an especially boring week what kind of exciting things <I>usually<\/I> happen to the Scripting Guys? Wow; where do we begin with a question like <I>that<\/I>? Let\u2019s see, the Scripting Guy who writes this column usually .. uh, writes this column. Scripting Guy Jean Ross usually \u2013 well, never mind. She didn\u2019t actually do that; she just saw it on TV. Scripting Guy Peter Costantini \u2013 OK, we\u2019re not sure about that: what exactly <I>do<\/I> you do with a package of Roman candles, a Speedo swimsuit, and two cartons of grated cheddar cheese? (That\u2019s OK; we\u2019d rather <I>not<\/I> know the answer to that question.) <\/P>\n<P>You know what? We\u2019ll have to get back to you on this.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I create a folder for each user in my domain? I\u2019d like the folder name to consist of the user\u2019s first initial and their last name (for example, KMyer).&#8212; GN Hey, GN. You know, this has been an interesting week for the Scripting Guys. Why? Because absolutely nothing interesting has [&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,168,3,20,5],"class_list":["post-64583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-domains","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I create a folder for each user in my domain? I\u2019d like the folder name to consist of the user\u2019s first initial and their last name (for example, KMyer).&#8212; GN Hey, GN. You know, this has been an interesting week for the Scripting Guys. Why? Because absolutely nothing interesting has [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64583","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=64583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64583\/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=64583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}