{"id":70603,"date":"2005-01-21T16:04:00","date_gmt":"2005-01-21T16:04:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/21\/how-can-i-find-and-move-an-active-directory-computer-account\/"},"modified":"2005-01-21T16:04:00","modified_gmt":"2005-01-21T16:04:00","slug":"how-can-i-find-and-move-an-active-directory-computer-account","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-find-and-move-an-active-directory-computer-account\/","title":{"rendered":"How Can I Find and Move an Active Directory Computer Account?"},"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! I need to move a computer account from one OU to another; however, I don\u2019t know which OU the computer account is currently in. Any advice?<BR><BR>&#8212; AA<\/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, AA. You know, one thing that marks all the great advice columns &#8211; <I>Dear Abby<\/I>; <I>Ann Landers<\/I>; <I>Hey, Scripting Guy!<\/I> &#8211; is the fact that, over time, these columns tend to give the same advice over and over. No matter how many times someone writes to Dear Abby complaining about problems with a neighbor, you know she\u2019s never going to suggest that you burn down the guy\u2019s house. The same is true with Ann Landers, and the same is true with <I>Hey, Scripting Guy!<\/I><\/P>\n<P>Well, OK: the same is true with Ann Landers.<\/P>\n<P>The point is, we often give the same advice in response to a reader\u2019s question, and today is no exception. So here goes: this is really a two-part problem, so let\u2019s analyze the two parts separately. Oh, and this: one of the parts involves searching Active Directory. Yes, we know: how many times have you heard <I>that<\/I>? But it seems to be what works.<\/P>\n<P>As you probably figured out, if one of the parts involves searching Active Directory, the other part must involve moving a computer account from one OU to another. Because this is so easy, let\u2019s go ahead and address it first. Here\u2019s a sample script that moves the computer atl-ws-01 from the Finance OU to the Research OU:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/OU=Research,DC=fabrikam,DC=com&#8221;)\nintReturn = objOU.MoveHere _\n    (&#8220;LDAP:\/\/CN=atl-ws-01,OU=Finance,DC=fabrikam,DC=com&#8221;, vbNullString)\n<\/PRE>\n<P>That\u2019s right: just two lines of code. We bind to the new OU (the one we want to move the computer to). After binding to the OU, we call the MoveHere method, passing it two parameters: the ADsPath of the computer we want to move; and vbNullString, a VBScript constant equal to Null. Passing a Null as the second parameter tells the MoveHere method that we want the object to keep its current CN (atl-ws-01). Had we passed a different CN, not only would the computer have been moved, but it would have been renamed as well.<\/P>\n<P>By the way, you don\u2019t actually have to include the Null parameter; if there\u2019s no second parameter MoveHere will assume the second parameter is Null. We show it here just so you know that MoveHere accepts two parameters.<\/P>\n<P><B>Special bonus script<\/B>: Suppose you <I>do<\/I> want to rename an Active Directory account. Well, then bind to the OU where the account currently lives, call the MoveHere method, and give the account a different CN. For example, this script renames the computer atl-ws-01, giving it the new name finance-ws-01:<\/P><PRE class=\"codeSample\">Set objOU = GetObject(&#8220;LDAP:\/\/OU=Finance,DC=fabrikam,DC=com&#8221;)\nintReturn = objOU.MoveHere _\n    (&#8220;LDAP:\/\/CN=atl-ws-01,OU=Finance,DC=fabrikam,DC=com&#8221;, \u201ccn=finance-ws-01\u201d)\n<\/PRE>\n<P>You\u2019re right: we should charge extra for these bonus scripts, shouldn\u2019t we?<\/P>\n<P>Now that we know how to move a computer account all we have to do is figure out how to <I>find<\/I> that computer account. Here\u2019s where we\u2019ll use our Active Directory search script. This script searches Active Directory for the computer named atl-ws-01:<\/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 ADsPath FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;computer&#8217; &#8221; &amp; _\n        &#8220;AND name=&#8217;atl-ws-01&#8242;&#8221;\nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    Wscript.Echo objRecordSet.Fields(&#8220;ADsPath&#8221;).Value\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>The preceding script merely echoes the ADsPath of the computer. That\u2019s nice, but as long as we have the AdsPath we can go ahead and move the computer account to a different OU. (Remember, unless we\u2019re renaming the computer, AdsPath is the only parameter we need to pass to the MoveHere method.) So here\u2019s a revised script that tracks down the computer atl-ws-01 and then moves it from the Finance OU to the Research OU:<\/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 ADsPath FROM &#8216;LDAP:\/\/dc=fabrikam,dc=com&#8217; WHERE objectCategory=&#8217;computer&#8217; &#8221; &amp; _\n        &#8220;AND name=&#8217;atl-ws-01&#8242;&#8221;\nSet objRecordSet = objCommand.Execute<\/p>\n<p>objRecordSet.MoveFirst\nDo Until objRecordSet.EOF\n    strADsPath = objRecordSet.Fields(&#8220;ADsPath&#8221;).Value\n    Set objOU = GetObject(&#8220;LDAP:\/\/OU=Research,DC=fabrikam,DC=com&#8221;)\n    intReturn = objOU.MoveHere(strADsPath, vbNullString)\n    objRecordSet.MoveNext\nLoop\n<\/PRE>\n<P>Just that easy, just that quick. <\/P>\n<P>Incidentally, we know we didn\u2019t explain how the Active Directory searching part works; for more information about using scripts to search Active Directory, check out this <A href=\"http:\/\/go.microsoft.com\/fwlink\/?LinkId=25562\" target=\"_blank\"><B>Scripting Guys<\/B><\/A> webcast.<\/P>\n<P><B>Tomorrow<\/B>: Burning down the neighbor\u2019s house.<\/P>\n<P>No, wait, don\u2019t burn down anyone\u2019s house: we\u2019re just <I>kidding<\/I> about that. Maybe we\u2019ll just stick to scripting advice from now on \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I need to move a computer account from one OU to another; however, I don\u2019t know which OU the computer account is currently in. Any advice?&#8212; AA Hey, AA. You know, one thing that marks all the great advice columns &#8211; Dear Abby; Ann Landers; Hey, Scripting Guy! &#8211; is the fact [&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,46,3,5],"class_list":["post-70603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-computer-accounts","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I need to move a computer account from one OU to another; however, I don\u2019t know which OU the computer account is currently in. Any advice?&#8212; AA Hey, AA. You know, one thing that marks all the great advice columns &#8211; Dear Abby; Ann Landers; Hey, Scripting Guy! &#8211; is the fact [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70603","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=70603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70603\/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=70603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}