{"id":68253,"date":"2006-01-04T15:17:00","date_gmt":"2006-01-04T15:17:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/04\/how-can-i-move-all-the-users-from-one-ou-to-another-ou\/"},"modified":"2006-01-04T15:17:00","modified_gmt":"2006-01-04T15:17:00","slug":"how-can-i-move-all-the-users-from-one-ou-to-another-ou","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-move-all-the-users-from-one-ou-to-another-ou\/","title":{"rendered":"How Can I Move All the Users from One OU to Another OU?"},"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 move all the users from one OU to another OU?<BR><BR>&#8212; PD<\/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, PD. You know, when you work at Microsoft you quickly learn to hate to word \u201cmove.\u201d After all, if it wasn\u2019t for the fact that we own the buildings you would think we were trying to skip out on paying the rent; that\u2019s how often we move from one place to another. On top of that, the moves we make often boggle the mind. For example, at one time Greg and Dean had offices right across the hall from one another; for some reason, the two were forced to exchange offices, leaving them, well, right across the hall from one another. Another time Greg was moved to a new office in a new building. Two months later was moved around the corner to a different office. Three months later he was moved back to his previous office! <\/P>\n<P>In case you\u2019re wondering, <I>that\u2019s<\/I> why we don\u2019t like the word move.<\/P>\n<P>But seeing as how all you want to do is move users from one OU to another (no packing and box-hauling required), well, <I>that<\/I> we can handle. Here\u2019s a script that moves all the users in the Accounting OU to the Finance OU:<\/P><PRE class=\"codeSample\">Set objNewOU = GetObject(&#8220;LDAP:\/\/ou=finance,dc=fabrikam,dc=com&#8221;)\nSet objOldOU = GetObject(&#8220;LDAP:\/\/ou=accounting,dc=fabrikam,dc=com&#8221;)<\/p>\n<p>objOldOU.Filter = Array(&#8220;User&#8221;)<\/p>\n<p>For Each objUser in objOldOU\n    objNewOU.MoveHere objUser.ADsPath, vbNullString\nNext\n<\/PRE>\n<P>You\u2019d think a chore like this would be more complicated, wouldn\u2019t you? But really, this is all the code you need. We start off by creating a pair of object references. The first one (objNewOU) binds us to the OU where we want to move the accounts:<\/P><PRE class=\"codeSample\">Set objNewOU = GetObject(&#8220;LDAP:\/\/ou=finance,dc=fabrikam,dc=com&#8221;)\n<\/PRE>\n<P>The second object reference (objOldOU) binds us to the OU where the accounts are currently housed:<\/P><PRE class=\"codeSample\">Set objOldOU = GetObject(&#8220;LDAP:\/\/ou=accounting,dc=fabrikam,dc=com&#8221;)\n<\/PRE>\n<P>Got that? It\u2019s pretty simple, but it\u2019s easy to get them mixed up. And you don\u2019t want to do that: otherwise you\u2019ll end up moving everyone from Finance to Accounting, which is <I>not<\/I> what you had in mind.<\/P>\n<P>As you know, when you bind to an OU in Active Directory you automatically get back a collection of all the objects found in that OU. In this case, though, we aren\u2019t interested in <I>all<\/I> the objects; the only objects we care about are the user accounts. With that in mind, we apply this filter to limit the items in the collection to user accounts:<\/P><PRE class=\"codeSample\">objOldOU.Filter = Array(&#8220;User&#8221;)\n<\/PRE>\n<P>This ensures that the only objects we\u2019ll be working with &#8211; and thus the only objects we\u2019ll be moving &#8211; are user accounts. If that\u2019s not a problem &#8211; that is, if you also want to move computer accounts, printers, and any other objects found in the Accounting OU &#8211; then just leave out the line of code that applies the filter.<\/P>\n<P>Now all we have to do is loop through the collection of user accounts and move each one to the Finance OU. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">For Each objUser in objOldOU\n    objNewOU.MoveHere objUser.ADsPath, vbNullString\nNext\n<\/PRE>\n<P>Notice <I>how<\/I> we move the user account; we call the <B>MoveHere<\/B> method, passing two parameters:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>objUser.ADsPath<\/B>. This, as you probably guessed, represents the ADsPath of the account to be moved.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>vbNullString<\/B>. This VBScript constant simply tells the script to move the account without renaming it. If we specified a different name here the account would not only be moved but would be renamed as well. <\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That\u2019s all there is to it.<\/P>\n<P>So does this mean the Scripting Guys are better able to handle the concept of moving? Let\u2019s put it this way: having been in our current offices for less than a year, there\u2019s a 50-50 chance we\u2019ll be moving again in January. The great circle of Microsoft life goes on. <I>(Editor\u2019s Note: This next move might not be a bad idea, because somehow in the last move the Scripting Editor ended up on a completely different floor of the building than the rest of the Scripting Guys. Although this editor suspects the other Scripting Guys &#8211; or at least Greg &#8211; may have had something to do with that\u2026.)<\/I><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I move all the users from one OU to another OU?&#8212; PD Hey, PD. You know, when you work at Microsoft you quickly learn to hate to word \u201cmove.\u201d After all, if it wasn\u2019t for the fact that we own the buildings you would think we were trying to skip [&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,43,3,20,5],"class_list":["post-68253","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-ous","tag-scripting-guy","tag-user-accounts","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I move all the users from one OU to another OU?&#8212; PD Hey, PD. You know, when you work at Microsoft you quickly learn to hate to word \u201cmove.\u201d After all, if it wasn\u2019t for the fact that we own the buildings you would think we were trying to skip [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68253","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=68253"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68253\/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=68253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}