{"id":68623,"date":"2005-11-01T14:22:00","date_gmt":"2005-11-01T14:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/11\/01\/how-can-i-change-a-users-samaccountname\/"},"modified":"2005-11-01T14:22:00","modified_gmt":"2005-11-01T14:22:00","slug":"how-can-i-change-a-users-samaccountname","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-change-a-users-samaccountname\/","title":{"rendered":"How Can I Change a User\u2019s sAMAccountName?"},"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 change the sAMAccountName from KMyer to Ken.Myer?<BR><BR>&#8212; CP<\/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, CP. You know, William Shakespeare once said, \u201cA rose by any other name would smell as sweet.\u201d What does that have to do with your question abut changing the sAMAccountName for a user? Nothing really; it\u2019s just that every now and then we like people to think that we\u2019re well-rounded, culturally-literate guys who do something besides watch baseball and <I>The Simpsons<\/I>. The truth is we\u2019re <I>not<\/I> culturally-literate guys who do something besides watch baseball and <I>The Simpsons<\/I>; we just don\u2019t want anyone to know that. <I>(Editor\u2019s Note: That\u2019s not true of all the Scripting Guys. Some of us watch football too.)<\/I><\/P>\n<P>If Shakespeare was alive today (he is dead, isn\u2019t he?) he\u2019d probably answer that question by saying something like, \u201cLeave me alone, Scripting Guys. I\u2019m tired of answering scripting questions for you; I\u2019ve got a play to write.\u201d But that\u2019s OK; fortunately, this is a question that we can answer ourselves, without any help from Mr. Shakespeare:<\/P><PRE class=\"codeSample\">Set objUser = GetObject(&#8220;cn=Ken Myer, ou=Finance, dc=Fabrikam, dc=com&#8221;)<\/p>\n<p>objUser.sAMAccountName = &#8220;Ken.Myer&#8221;\nobjUser.userPrincipalName = &#8220;Ken.Myer&#8221;\nobjUser.SetInfo\n<\/PRE>\n<P>Granted, it\u2019s not iambic pentameter (or maybe it is; we aren\u2019t totally sure what iambic pentameter looks like) but there\u2019s still a certain beauty to the code, don\u2019t you think? The script starts off by binding to the Ken Myer user account in Active Directory. We then use this line of code to change the sAMAccountName to <B>Ken.Myer<\/B>:<\/P><PRE class=\"codeSample\">objUser.sAMAccountName = &#8220;Ken.Myer&#8221;\n<\/PRE>\n<P>Now, we\u2019re not saying that the Scripting Guys are better than Shakespeare; all we\u2019re saying is that if you attend one of Shakespeare\u2019s plays he doesn\u2019t give you a bonus play absolutely free. The Scripting Guys, however, give you bonus code absolutely free. Although the following line of code is optional, many organizations like to keep their UPNs (user principal names) in sync with their sAMAccountNames; that way users have to remember only one name &#8211; Ken.Myer &#8211; as opposed to two: Ken.Myer and kmyer@fabrikam.com. Therefore, we tossed in this line of code to change the UPN as well:<\/P><PRE class=\"codeSample\">objUser.userPrincipalName = &#8220;Ken.Myer&#8221;\n<\/PRE>\n<P>Last, but definitely not least, we call the <B>SetInfo<\/B> method to write the changes back to Active Directory:<\/P><PRE class=\"codeSample\">objUser.SetInfo\n<\/PRE>\n<P>Whatever you do, don\u2019t leave out this line of code. When you bind to a user account in Active Directory, a <I>copy<\/I> of that account is created in memory on your machine. As you make changes to the account properties, those changes are made on this copy; the original account remains in Active Directory, unchanged. The only way to update the actual user account, the one in Active Directory, is to call the SetInfo method, which is roughly equivalent to clicking the <B>Save<\/B> button in, say, Microsoft Word.<\/P>\n<P>So there you have it, CP. As William Shakespeare once said, \u201cSee you tomorrow.\u201d<\/P>\n<TABLE id=\"E4D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Hey, he probably said that at <I>some<\/I> point in his life.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I change the sAMAccountName from KMyer to Ken.Myer?&#8212; CP Hey, CP. You know, William Shakespeare once said, \u201cA rose by any other name would smell as sweet.\u201d What does that have to do with your question abut changing the sAMAccountName for a user? Nothing really; it\u2019s just that every now [&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-68623","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>Hey, Scripting Guy! How can I change the sAMAccountName from KMyer to Ken.Myer?&#8212; CP Hey, CP. You know, William Shakespeare once said, \u201cA rose by any other name would smell as sweet.\u201d What does that have to do with your question abut changing the sAMAccountName for a user? Nothing really; it\u2019s just that every now [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68623","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=68623"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68623\/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=68623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}