How Can I Change a User’s sAMAccountName?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I change the sAMAccountName from KMyer to Ken.Myer?

-- CP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, CP. You know, William Shakespeare once said, “A rose by any other name would smell as sweet.” What does that have to do with your question abut changing the sAMAccountName for a user? Nothing really; it’s just that every now and then we like people to think that we’re well-rounded, culturally-literate guys who do something besides watch baseball and The Simpsons. The truth is we’re not culturally-literate guys who do something besides watch baseball and The Simpsons; we just don’t want anyone to know that. (Editor’s Note: That’s not true of all the Scripting Guys. Some of us watch football too.)

If Shakespeare was alive today (he is dead, isn’t he?) he’d probably answer that question by saying something like, “Leave me alone, Scripting Guys. I’m tired of answering scripting questions for you; I’ve got a play to write.” But that’s OK; fortunately, this is a question that we can answer ourselves, without any help from Mr. Shakespeare:

Set objUser = GetObject("cn=Ken Myer, ou=Finance, dc=Fabrikam, dc=com")

objUser.sAMAccountName = "Ken.Myer"
objUser.userPrincipalName = "Ken.Myer"

Granted, it’s not iambic pentameter (or maybe it is; we aren’t totally sure what iambic pentameter looks like) but there’s still a certain beauty to the code, don’t 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 Ken.Myer:

objUser.sAMAccountName = "Ken.Myer"

Now, we’re not saying that the Scripting Guys are better than Shakespeare; all we’re saying is that if you attend one of Shakespeare’s plays he doesn’t 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 - Ken.Myer - as opposed to two: Ken.Myer and Therefore, we tossed in this line of code to change the UPN as well:

objUser.userPrincipalName = "Ken.Myer"

Last, but definitely not least, we call the SetInfo method to write the changes back to Active Directory:


Whatever you do, don’t leave out this line of code. When you bind to a user account in Active Directory, a copy 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 Save button in, say, Microsoft Word.

So there you have it, CP. As William Shakespeare once said, “See you tomorrow.”

Note. Hey, he probably said that at some point in his life.

Comments (6)

  1. Dacre says:

    Set objUser = GetObject("cn=Ken Myer, ou=Finance, dc=Fabrikam, dc=com")

    Should be;

    Set objUser = GetObject("LDAP://cn=Ken Myer, ou=Finance, dc=Fabrikam, dc=com")

  2. KC says:

    I need to do this for about 4100 users.  I already have a spreadsheet with the new samaccountname (and have imported it into a test domain and OU)

    I'm not a scripting guy at all, but have been able to use examples from this site and others to get a multitude of IT things accomplished.  This once is beyond my scope of knowledge.


  3. RoNin says:

    It always throws me an error:

    "Set-Variable : A positional parameter cannot be found that accepts argument 'GetObject'."

    What's wrong?

  4. Max says:

    Hi KC

    I’m in need of renaming thousands of accounts’ samaccountname to the new one.

    Could you please help with any script.

  5. Silvio Reis says:

    Just a comment: If you need to rename a samAccountName with special characters like c-cedil n-tilde a-tilde (ç ã ) you can´t rename from façade to facade in one move, you must rename façade to facade2 and then rename again from facade2 to facade.

Skip to main content