{"id":70963,"date":"2004-11-19T11:09:00","date_gmt":"2004-11-19T11:09:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/11\/19\/how-can-i-put-new-users-in-the-same-ou-as-the-person-creating-their-accounts\/"},"modified":"2004-11-19T11:09:00","modified_gmt":"2004-11-19T11:09:00","slug":"how-can-i-put-new-users-in-the-same-ou-as-the-person-creating-their-accounts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-put-new-users-in-the-same-ou-as-the-person-creating-their-accounts\/","title":{"rendered":"How Can I Put New Users in the Same OU as the Person Creating Their Accounts?"},"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! We use a script to create user accounts. How can I modify this script so that it will place the new user in the same OU as the person running the script?<BR><BR>&#8212; CB<\/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, CB. Interesting question. We\u2019re assuming that you\u2019ve delegated control of Active Directory. User A, for example, has the right to create users in the Finance OU, but <I>only<\/I> the Finance OU. User B, meanwhile, can also create user accounts, but only in the Headquarters OU. If you\u2019re like other people we know in this situation you\u2019ve done one of two things: either you\u2019ve created a generic script that prompts the user to enter the OU each time they create a user account, or you\u2019ve created separate scripts for each user who has been authorized to create accounts.<\/P>\n<P>Unfortunately, neither of these approaches is optimal. The first one requires the user to type the Active Directory path each and every time they create a user account; the second one requires you to maintain, distribute, update, and otherwise take care of multiple scripts, all of which ultimately carry out the same exact task (they create user accounts). You\u2019re looking for a single script that can say, \u201cOh, you\u2019re User A, huh? Well, I know that you\u2019re in Finance OU, so I\u2019ll automatically create this new user account in that same OU.\u201d<\/P>\n<P>And good news: we just happened to have a script that will do this very thing. In fact, here it is:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Set objSysInfo = CreateObject(&#8220;ADSystemInfo&#8221;)<\/p>\n<p>arrDirectoryLocation =  Split(objSysInfo.UserName, &#8220;,&#8221;)<\/p>\n<p>For i = 1 to Ubound(arrDirectoryLocation)\n    If i = 1 Then\n        strLocation = arrDirectoryLocation(i)\n    Else\n        strLocation = strLocation &amp; &#8220;,&#8221; &amp; arrDirectoryLocation(i)\n    End If\nNext<\/p>\n<p>strOU = &#8220;LDAP:\/\/&#8221; &amp; strLocation<\/p>\n<p>Set objOU = GetObject(strOU) \nSet objUser = objOU.Create(&#8220;User&#8221;, &#8220;cn=Myer Ken&#8221;) \nobjUser.sAMAccountName = &#8220;myerken&#8221; \nobjUser.GivenName = &#8220;Ken&#8221; \nobjUser.SN = &#8220;Myer&#8221; \nobjUser.AccountDisabled = FALSE \nobjUser.SetInfo\n<\/PRE>\n<P>Admittedly, it might not be immediately obvious what\u2019s going on here. So let\u2019s take a minute or two to walk you through the process.<\/P>\n<P>The script starts by creating an instance of the ADSystemInfo object; this object can return basic Active Directory information for the logged on user and his or her computer, including the user\u2019s Distinguished Name (the UserName property). In other words, we can get back something that looks like this:<\/P><PRE class=\"codeSample\">CN=&#8221;Jonathan Haas&#8221;, OU=&#8221;Finance&#8221;,DC=&#8221;fabrikam&#8221;,DC=&#8221;com&#8221;\n<\/PRE>\n<P>As you can see, this is <I>close<\/I> to the information we need; if we can get rid of the CN=\u201dJonathan Hass\u201d we\u2019ll have a path to the desired OU. And before you ask, no, we can\u2019t just ask for the value of the user\u2019s OU; for some reason, Active Directory doesn\u2019t store that information directly. Because of that, we\u2019ll have to figure out the OU path on our own. But trust us, that\u2019s easy.<\/P>\n<P>We begin the process with this line of code:<\/P><PRE class=\"codeSample\">arrDirectoryLocation =  Split(objSysInfo.UserName, &#8220;,&#8221;)\n<\/PRE>\n<P>This code takes the user\u2019s Distinguished Name and &#8211; thanks to the magic of the Split function &#8211; creates an array out of that name. Our new array (which divvies up the items using the comma as a delimiter) looks like this:<\/P><PRE class=\"codeSample\">CN=&#8221;Jonathan Haas&#8221;\nOU=&#8221;Finance&#8221;\nDC=&#8221;fabrikam&#8221;\nDC=&#8221;com&#8221;\n<\/PRE>\n<P>What did that gain us? Well, now we have an array with four elements. If we get rid of the first element (Jonathan Haas, item 0) we can then construct the OU path using the remaining elements. To do that, we\u2019re simply going to loop through array beginning with the <I>second<\/I> element (also known as item 1; remember the first element in an array is item 0, making the second element item 1). That\u2019s what this code does: it bypasses the first element (CN=\u201cJonathan Haas\u201d) and then grabs each of the remaining elements until there\u2019s nothing left to grab (Ubound represents the last item in an array):<\/P><PRE class=\"codeSample\">For i = 1 to Ubound(arrDirectoryLocation)\n<\/PRE>\n<P>Inside the loop we reconstruct the OU path, jamming all the names back together and separating them with commas. You might notice that, when dealing with item 1, we don\u2019t add a comma before tacking on the element value. If we did, we\u2019d end up with a string that looked like this:<\/P><PRE class=\"codeSample\">, OU=&#8221;Finance&#8221;,DC=&#8221;fabrikam&#8221;,DC=&#8221;com&#8221;\n<\/PRE><PRE class=\"codeSample\">Not what we want. By omitting the comma for the first item, we end up with this path:\n<\/PRE><PRE class=\"codeSample\">OU=&#8221;Finance&#8221;,DC=&#8221;fabrikam&#8221;,DC=&#8221;com&#8221;\n<\/PRE>\n<P>Believe it or not, we\u2019re home free now. To construct the ADsPath to the OU, we just need to tack on the provider name. By adding LDAP:\/\/ to the beginning of our string, we end up with a path that looks just like this:<\/P><PRE class=\"codeSample\">LDAP:\/\/OU=&#8221;Finance&#8221;,DC=&#8221;fabrikam&#8221;,DC=&#8221;com&#8221;\n<\/PRE>\n<P>And guess what? By remarkable coincidence, this is exact information we need to bind to the OU and create the new user account. In other words:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(strOU)\n<\/PRE>\n<P>The rest of the script simply creates the new user account, sets a few property values, and then uses SetInfo to save the new account to Active Directory. We had to go through a few gyrations to get there, but you now have a generic script that will create new user accounts in the same OU as the user running the script.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We use a script to create user accounts. How can I modify this script so that it will place the new user in the same OU as the person running the script?&#8212; CB Hey, CB. Interesting question. We\u2019re assuming that you\u2019ve delegated control of Active Directory. User A, for example, has the [&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,9,3,4,20,5],"class_list":["post-70963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-adsi","tag-scripting-guy","tag-scripting-techniques","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We use a script to create user accounts. How can I modify this script so that it will place the new user in the same OU as the person running the script?&#8212; CB Hey, CB. Interesting question. We\u2019re assuming that you\u2019ve delegated control of Active Directory. User A, for example, has the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70963","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=70963"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70963\/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=70963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}