How Can I Rename a Local User Account on a Windows XP Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I rename a local user account on a Windows XP computer?

-- TY

SpacerHey, Scripting Guy! AnswerScript Center

Hey, TY. You know, nowadays the “in” thing is to poke fun at Microsoft and Windows XP. We know: we’ve seen the new commercials from that one computer company. (Remember, the computer company that used to be good, a long time ago?) But as much as we Scripting Guys would like to be thought of as cool we’re not going to make fun of Windows XP today; in fact, we’re going to sing its praises. Is that because Microsoft signs our paycheck? Well, OK, sure. But in addition to that, Windows XP (and Windows Server 2003) makes it possible for you to use a script to rename local user accounts. That might not be cool, but it is useful.

Which is almost as good.

Here - in all its uncool glory - is the script we came up with:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colAccounts = objWMIService.ExecQuery _
("Select * From Win32_UserAccount Where LocalAccount = True And Name = 'kmyer'")

For Each objAccount in colAccounts
objAccount.Rename "kenmyer"

The first thing you might have noticed about this script is that it uses WMI instead of ADSI. Wait a second, you’re thinking: don’t the Scripting Guys always tell us to use ADSI when it comes to managing users and groups? Yes, we do, and that’s exactly what you should do … most of the time. Renaming local user accounts happens to be a rare exception, however. With ADSI you need to use the MoveHere method to rename user accounts. That’s fine, except for the fact that MoveHere only works for domain accounts; if you try renaming a local user account you’ll get the much-beloved “unspecified error” error message. The unspecified error: you can’t use MoveHere to rename a local user account.

Fortunately, though, you can use WMI to rename local user accounts, provided you’re running Windows XP or Windows Server 2003. Why just those two versions of Windows? That’s because those are the only versions in which the Win32_UserAccount class includes the Rename method. You can’t use WMI to rename local user accounts on a Windows 2000 computer because the Rename method doesn’t exist in Windows 2000.

And, no, at this point in time there’s probably no chance that this capability will be added to Windows 2000.

But you don’t care about that do you, TY, because you specifically asked about performing this task on a Windows XP computer. With that in mind, let’s examine the code in a little more detail, beginning with the fact that the script starts out by connecting to the WMI service on the local computer. What if the account we wanted to rename happens to reside on a remote computer? No problem; just set the value of the variable strComputer to the name of the remote machine.

Ah, come on: that’s at least kind of cool, isn’t it? Well, some of the Scripting Moms thought it was kind of cool.

After making the connection to the WMI service we next encounter this line of code:

Set colAccounts = objWMIService.ExecQuery _
("Select * From Win32_UserAccount Where LocalAccount = True And Name = 'kmyer'")

What we’re doing here is retrieving a collection of all the user accounts that we want to rename. Of course, there’s only one account we want to rename: the local account kmyer. Hence our Where clause, which asks the script to “select all the accounts where the value of the LocalAccount property is true (meaning this is a local account rather than a domain account) and where the account Name is equal to kmyer.” Because account names must be unique on a computer, that means this query will return, at most, a single item.

Note. Whatever you do, don’t leave off LocalAccount = True. If you do the script will not only retrieve and sort through all your local user accounts, but all your Active Directory user accounts as well. That could take awhile to complete; in addition, it could lead to problems if you happen to have an Active Directory user account named kmyer.

Believe it or not we’re almost done. All we have left to do is set up a For Each loop to cycle through all the items in the collection. Inside the loop we use the Rename method to rename the user account, in this case changing the account name to kenmyer:

For Each objAccount in colAccounts
objAccount.Rename "kenmyer"

That’s it: just like that the account will be renamed. The next time Ken Myer logs on to the local computer he’ll need to log on as kenmyer rather than kmyer.

And, yes, you can indeed use this script to rename the local Administrator account. For example, this revised code renames the Administrator account to NotAdministrator:

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAccounts = objWMIService.ExecQuery _
("Select * From Win32_UserAccount Where LocalAccount = True And Name = 'Administrator'")

For Each objAccount in colAccounts
objAccount.Rename "NotAdministrator"

Now that is cool!

Come to think of it, you don’t suppose anyone would be interested in a commercial about using scripts to rename local user accounts, do you? Good point. But there’s probably not much chance we could get Microsoft to spring for a bunch of bikini- and/or Speedo-clad extras.

Comments (9)

  1. Anonymous says:

    how would this be applied to all computers in an OU?

  2. Lorene says:

    i reset my pass word and know it wount let me under my name and i am the main acount of the compture

  3. Pat says:

    Wow!! way to complicated for a non tech person

  4. ...1 says:

    …yeah i have no idea what you are talking about.

  5. Ed Wilson says:

    @sfrazier2 you would need to query AD for all the commputers in an OU, then walk through the array of computers and call this script.

    @pat this is written for a network administrator, or for some other IT Pro. It is not aimed at a non-technical person.

    Here is an easier approach using Windows PowerShell…/use-a-free-powershell-module-to-work-with-local-accounts.aspx

  6. Adai says:

    The script given in this article works.. I have also found that we can rename user account using wmi. source:…/rename-local-user-account.html. This also seems to be working. Does the script have any advantage? Why to use the script(though it's short one) if we can do the same using existing wmic command in a single line?

  7. Ed Wilson says:

    @adai You are correct you can do this using WMIC. In fact, WMIC uses exactly the same WMI class that this particular script uses. This script was also written more than five years ago. The emphasis of the blog at that time was to teach VBScript, and not to teach WMIC. There are other resources devoted to WMIC. The blog now focuses on Windows PowerShell which is way easier than VBScript, and is also Microsoft's strategic automation direction going forward. I could use this methodology in Windows PowerShell, and it would also be a single line of code.

  8. looda says:

    oh  huge artical   is there is prog for it to make it easiar?   thx

  9. meffordm says:

    Ed, thanks for all these blogs. They answer so many questions.

Skip to main content