{"id":69333,"date":"2005-07-22T14:41:00","date_gmt":"2005-07-22T14:41:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/22\/how-can-i-determine-if-the-local-administrator-account-has-been-renamed-on-a-computer\/"},"modified":"2005-07-22T14:41:00","modified_gmt":"2005-07-22T14:41:00","slug":"how-can-i-determine-if-the-local-administrator-account-has-been-renamed-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-if-the-local-administrator-account-has-been-renamed-on-a-computer\/","title":{"rendered":"How Can I Determine if the Local Administrator Account has been Renamed on a 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 determine if the local administrator account has been renamed on a computer?<BR><BR>&#8212; KF<\/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, KF. You know, our first thought was to tell you to use ADSI and try to bind to the local administrator account on the computer; if that failed, that would mean that there <I>was<\/I> no account named Administrator on that computer. In turn, that would mean that the account had either been renamed or deleted.<\/P>\n<P>But then we thought about this a little more. (Yes, having two thoughts in a single day <I>is<\/I> a Scripting Guys record.) Suppose the local administrator account <I>had<\/I> been renamed. In that case, you\u2019d probably want to know the new name for the account. So what we <I>really<\/I> want to know is the name of the local administrator account. Period.<\/P>\n<P>But how can we know the name of the account if the account has been renamed? (This is the scripting equivalent of, \u201cIf a tree falls in the forest and no one is there to hear it, does it still make a sound?\u201d) Why, through the magic of the \u201cwell-known SID,\u201d of course. A SID, as you probably know, is a \u201csecurity identifier,\u201d a unique identifier assigned to each account on a computer. The computer actually uses the SID to keep track of each account: if you rename the administrator account the computer still knows which account is the administrator account. That\u2019s because the SID &#8211; unlike the name &#8211; never changes.<\/P>\n<P>Now that sounds pretty good except for one thing: if the SID is a unique identifier then how can we determine which SID represents the administrator account? That\u2019s where the \u201cwell-known\u201d part comes in. On a computer the SID for a local administrator will <I>always<\/I> begin with <B>S-1-5-<\/B> and end with <B>-500<\/B>. (That\u2019s why the administrator SID-and other SIDS, such as SIDs for the Guest account-are considered well-known.) For example, you might have a SID that looks like this:<\/P><PRE class=\"codeSample\">S-1-5-21-1559272821-92556266-1055285598-500\n<\/PRE>\n<P>As you can see, our SID starts with S-1-5- and ends with -500. If we can find a SID that fits that pattern, then we\u2019ve found our local administrator account. <\/P>\n<P>So <I>can<\/I> we find a SID that fits that pattern? Yes, by using a script like this:<\/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&#8221;)<\/p>\n<p>For Each objAccount in colAccounts\n    If Left (objAccount.SID, 6) = &#8220;S-1-5-&#8221; and Right(objAccount.SID, 4) = &#8220;-500&#8221; Then\n        Wscript.Echo objAccount.Name\n    End If\nNext\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"E2D\" 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>. This particular script works only on Windows XP and Windows Server 2003. In a minute we\u2019ll show you a way to perform this same task on Windows 2000 Server or Windows NT Server 4.0.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We begin by connecting to the WMI service on a computer, then use this query to retrieve a list of local computer accounts:<\/P><PRE class=\"codeSample\">Set colAccounts = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_UserAccount Where LocalAccount = TRUE&#8221;)\n<\/PRE>\n<P>Note the use of the Where clause <B>Where LocalAccount = TRUE<\/B>. That ensures that we get back only local accounts; we aren\u2019t interested in weeding through domain accounts. This is also the reason why this particular script won\u2019t run on Windows 2000 or Windows NT 4.0; the LocalAccount property wasn\u2019t introduced until Windows XP.<\/P>\n<P>After getting back a collection of local user accounts we set up a For Each loop and walk through the collection. For each account we use this line of code to see if the account fits the well-known SID pattern:<\/P><PRE class=\"codeSample\">If Left (objAccount.SID, 6) = &#8220;S-1-5-&#8221; and Right(objAccount.SID, 4) = &#8220;-500&#8221; Then\n<\/PRE>\n<P>As you can see, we simply check to see if the first six characters in the string equal <B>S-1-5- <\/B><I>and<\/I> if the last four characters equal <B>-500<\/B>. If they do then we\u2019ve found the local administrator account and we echo the account name. If they don\u2019t, then we loop around and check the next account in the collection.<\/P>\n<P>That should do the trick, provided that you\u2019re running the script on Windows XP or Windows Server 2003. If you\u2019re running on Windows 2000 Server or Windows NT Server 4.0 you can use this variation. (This version will also work on Windows XP and Windows Server 2003.) In this script we begin by assigning the name of the computer to the variable strComputer; note, too that we need to use the actual computer name and not the dot (a WMI shortcut method for indicating the local machine). We have to use the computer name because we\u2019ll use this variable to look for accounts with a \u201cdomain\u201d that matches the computer name. (It\u2019s confusing, but in a technical sense a computer is a domain, though obviously not on the scale of something like Active Directory.) Although a dot might represent the local computer name in WMI, it does <I>not<\/I> represent a domain name. Therefore, we must be very explicit when specifying the computer name.<\/P>\n<P>And, yes, when dealing with a local computer account the domain name is equivalent to the computer name. If you have a user named kmyer and a computer named atl-ws-01, the domain name is atl-ws-01 and the user name is kmyer. <\/P>\n<P>We then use a modified query to return a collection of accounts where the <B>Domain<\/B> is equal to the name of the local machine; that will eliminate Active Directory accounts and return only local accounts.<\/P>\n<P>Here\u2019s the script:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#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 Domain = &#8216;&#8221; &amp; strComputer &amp; &#8220;&#8216;&#8221;)<\/p>\n<p>For Each objAccount in colAccounts\n    If Left (objAccount.SID, 6) = &#8220;S-1-5-&#8221; and Right(objAccount.SID, 4) = &#8220;-500&#8221; Then\n        Wscript.Echo objAccount.Name\n    End If\nNext\n<\/PRE>\n<P>Why are we so concerned with limiting the returned collection to local accounts? Well, without that clause the <B>Win32_UserAccount<\/B> class will try to return a list of all the user accounts in Active Directory as well as all the local user accounts. If you have an hour or two (or three) to kill, well, what the heck: go ahead and return &#8211; and weed through &#8211; several thousand user accounts if you want to. Otherwise you\u2019re better off limiting the returned data to local accounts only. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine if the local administrator account has been renamed on a computer?&#8212; KF Hey, KF. You know, our first thought was to tell you to use ADSI and try to bind to the local administrator account on the computer; if that failed, that would mean that there was no [&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-69333","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 determine if the local administrator account has been renamed on a computer?&#8212; KF Hey, KF. You know, our first thought was to tell you to use ADSI and try to bind to the local administrator account on the computer; if that failed, that would mean that there was no [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69333","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=69333"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69333\/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=69333"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69333"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69333"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}