{"id":67303,"date":"2006-05-17T19:41:00","date_gmt":"2006-05-17T19:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/05\/17\/how-can-i-rename-a-local-user-account-on-a-windows-xp-computer\/"},"modified":"2006-05-17T19:41:00","modified_gmt":"2006-05-17T19:41:00","slug":"how-can-i-rename-a-local-user-account-on-a-windows-xp-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-rename-a-local-user-account-on-a-windows-xp-computer\/","title":{"rendered":"How Can I Rename a Local User Account on a Windows XP Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I rename a local user account on a Windows XP computer?<BR><BR>&#8212; TY<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, TY. You know, nowadays the \u201cin\u201d thing is to poke fun at Microsoft and Windows XP. We know: we\u2019ve 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\u2019re not going to make fun of Windows XP today; in fact, we\u2019re 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 <I>is<\/I> useful.<\/P>\n<P>Which is almost as good.<\/P>\n<P>Here &#8211; in all its uncool glory &#8211; is the script we came up with:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colAccounts = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_UserAccount Where LocalAccount = True And Name = &#8216;kmyer'&#8221;)<\/p>\n<p>For Each objAccount in colAccounts\n    objAccount.Rename &#8220;kenmyer&#8221;\nNext\n<\/PRE>\n<P>The first thing you might have noticed about this script is that it uses WMI instead of ADSI. Wait a second, you\u2019re thinking: don\u2019t the Scripting Guys always tell us to use ADSI when it comes to managing users and groups? Yes, we do, and that\u2019s exactly what you should do \u2026 <I>most<\/I> 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\u2019s fine, except for the fact that MoveHere only works for domain accounts; if you try renaming a local user account you\u2019ll get the much-beloved \u201cunspecified error\u201d error message. The unspecified error: you can\u2019t use MoveHere to rename a local user account.<\/P>\n<P>Fortunately, though, you <I>can<\/I> use WMI to rename local user accounts, provided you\u2019re running Windows XP or Windows Server 2003. Why just those two versions of Windows? That\u2019s because those are the only versions in which the <B>Win32_UserAccount<\/B> class includes the <B>Rename<\/B> method. You can\u2019t use WMI to rename local user accounts on a Windows 2000 computer because the Rename method doesn\u2019t exist in Windows 2000.<\/P>\n<P>And, no, at this point in time there\u2019s probably no chance that this capability will be added to Windows 2000.<\/P>\n<P>But you don\u2019t care about that do you, TY, because you specifically asked about performing this task on a Windows XP computer. With that in mind, let\u2019s 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.<\/P>\n<P>Ah, come on: that\u2019s at least <I>kind of<\/I> cool, isn\u2019t it? Well, some of the Scripting Moms thought it was kind of cool.<\/P>\n<P>After making the connection to the WMI service we next encounter this line of code:<\/P><PRE class=\"codeSample\">Set colAccounts = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_UserAccount Where LocalAccount = True And Name = &#8216;kmyer'&#8221;)\n<\/PRE>\n<P>What we\u2019re doing here is retrieving a collection of all the user accounts that we want to rename. Of course, there\u2019s only one account we want to rename: the local account kmyer. Hence our Where clause, which asks the script to \u201cselect all the accounts where the value of the <B>LocalAccount<\/B> property is true (meaning this is a local account rather than a domain account) <I>and<\/I> where the account <B>Name<\/B> is equal to kmyer.\u201d Because account names must be unique on a computer, that means this query will return, at most, a single item.<\/P>\n<TABLE class=\"dataTable\" id=\"EDE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Whatever you do, don\u2019t leave off <B>LocalAccount = True<\/B>. 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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Believe it or not we\u2019re 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:<\/P><PRE class=\"codeSample\">For Each objAccount in colAccounts\n    objAccount.Rename &#8220;kenmyer&#8221;\nNext\n<\/PRE>\n<P>That\u2019s it: just like that the account will be renamed. The next time Ken Myer logs on to the local computer he\u2019ll need to log on as kenmyer rather than kmyer.<\/P>\n<P>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:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colAccounts = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_UserAccount Where LocalAccount = True And Name = &#8216;Administrator'&#8221;)<\/p>\n<p>For Each objAccount in colAccounts\n    objAccount.Rename &#8220;NotAdministrator&#8221;\nNext\n<\/PRE>\n<P>Now that <I>is<\/I> cool!<\/P>\n<P>Come to think of it, you don\u2019t suppose anyone would be interested in a commercial about using scripts to rename local user accounts, do you? Good point. But there\u2019s probably not much chance we could get Microsoft to spring for a bunch of bikini- and\/or Speedo-clad extras.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I rename a local user account on a Windows XP computer?&#8212; TY Hey, TY. You know, nowadays the \u201cin\u201d thing is to poke fun at Microsoft and Windows XP. We know: we\u2019ve seen the new commercials from that one computer company. (Remember, the computer company that used to be good, [&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":[23,24,3,5],"class_list":["post-67303","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-local-accounts-and-windows-nt-4-0-accounts","tag-other-directory-services","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I rename a local user account on a Windows XP computer?&#8212; TY Hey, TY. You know, nowadays the \u201cin\u201d thing is to poke fun at Microsoft and Windows XP. We know: we\u2019ve seen the new commercials from that one computer company. (Remember, the computer company that used to be good, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67303","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=67303"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67303\/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=67303"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67303"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67303"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}