{"id":68353,"date":"2005-12-12T13:30:00","date_gmt":"2005-12-12T13:30:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/12\/12\/how-can-i-delete-everyone-except-the-administrator-and-the-domain-admins-group-from-the-local-administrators-group\/"},"modified":"2005-12-12T13:30:00","modified_gmt":"2005-12-12T13:30:00","slug":"how-can-i-delete-everyone-except-the-administrator-and-the-domain-admins-group-from-the-local-administrators-group","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-delete-everyone-except-the-administrator-and-the-domain-admins-group-from-the-local-administrators-group\/","title":{"rendered":"How Can I Delete Everyone Except the Administrator and the Domain Admins Group from the Local Administrators Group?"},"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 delete everyone except the Administrator and the Domain Admins group from the local Administrators group?<BR><BR>&#8212; JS<\/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, JS. You know, when you become a doctor you have to take the Hippocratic Oath, which famously begins, \u201cFirst, do no harm.\u201d When you become a Scripting Guy you have to take the Scriptocratic Oath, an oath which, somewhat less-famously, begins, \u201cFirst, give them a warning. After that it\u2019s not your fault if anything bad happens.\u201d So, JS, consider yourself warned.<\/P>\n<P>Actually, the script we\u2019re about to show you isn\u2019t particularly dangerous. However, it <I>could<\/I> be a bit of a nuisance. Per your request, the script removes everyone except the Administrator and Domain Admin accounts from the local Administrators group. That\u2019s fine, except in at least one scenario. At Microsoft, for example, users are typically local administrators on their computers. However, those users never log on as the local Administrator; instead, they log on using their domain account, which happens to be a member of the local Administrators group.<\/P>\n<P>So what\u2019s the problem? Well, the script we\u2019re about to show you will remove that domain user account from the local Administrators group; as a result, those users will no longer be local Administrators. That might very well be what you <I>want<\/I> to happen. But forewarned is forearmed and all that.<\/P>\n<TABLE id=\"ECD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>True story<\/B>. Not too long ago, one of the Scripting Guys had to temporarily remove their computer from the domain. They did so, a process which also removed their domain user account &#8211; and the Domain Admins account &#8211; from the local Administrators group. Of course, you could still log on as Administrator \u2026 provided you knew the local Administrators password, that is. As you might have guessed, this Scripting Guy had no idea what the local Administrators password was. Uh-oh \u2026.<\/P>\n<P>And, no, we can\u2019t tell you <I>which<\/I> Scripting Guy did this: Jean would be terribly embarrassed if anyone ever found out.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In other words, don\u2019t run with scissors, don\u2019t stick your finger in a light socket, and don\u2019t use this script if it\u2019s going to lock you out of your own machine:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;<\/p>\n<p>Set objGroup = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Administrators&#8221;)<\/p>\n<p>For Each objUser In objGroup.Members\n    If objUser.Name &lt;&gt; &#8220;Administrator&#8221; AND objUser.Name &lt;&gt; &#8220;Domain Admins&#8221; Then\n        objGroup.Remove(objUser.AdsPath)\n    End If\nNext\n<\/PRE>\n<P>Yes, it <I>does<\/I> look harmless, doesn\u2019t it? And it is: after all, a domain Administrator can still access the machine and add someone back to the local Administrators group. It could create a nuisance for you, but nothing that can\u2019t be fixed.<\/P>\n<P>As for the script itself, it begins by connecting to the Administrators group on a specified computer; in this sample script, that\u2019s a computer named atl-ws-01. After making the connection the script sets up a For Each loop to loop through all the members of the group; said membership can be accessed via the <B>Members<\/B> property.<\/P>\n<P>Inside the loop we use this line of code to ensure that the name of our group member is neither Administrator nor Domain Admins:<\/P><PRE class=\"codeSample\">If objUser.Name &lt;&gt; &#8220;Administrator&#8221; AND objUser.Name &lt;&gt; &#8220;Domain Admins&#8221; Then\n<\/PRE>\n<P>Notice that we use the AND operator here: the Name is not Administrator <I>and<\/I> the Name is not Domain Admins. A common mistake scripters make is to use the OR operator in a script like this: the Name is not Administrator <I>or<\/I> the Name is not Domain Admins. Don\u2019t make that mistake. <\/P>\n<P>Why not? That\u2019s easy: because then <I>every<\/I> member of the group will fit the criteria. Take the Administrator account, for example. Granted, the name of the account <I>is<\/I> equal to Administrator; that would seem to disqualify it. However, the name is <I>not<\/I> equal to Domain Admins; consequently it <I>does<\/I> meet the criteria; after all, you qualify if the name is not equal to Administrator <I>or<\/I> the name is not equal to Domain Admins. That\u2019s why we make sure that the name is not equal to Administrator <I>and<\/I> it is not equal to Domain Admins. <\/P>\n<P>If you don\u2019t see how this works try running this script, which simply reports back the names of the group members:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-ws-01&#8221;<\/p>\n<p>Set objGroup = GetObject(&#8220;WinNT:\/\/&#8221; &amp; strComputer &amp; &#8220;\/Administrators&#8221;)<\/p>\n<p>For Each objUser In objGroup.Members\n    If objUser.Name &lt;&gt; &#8220;Administrator&#8221; AND objUser.Name &lt;&gt; &#8220;Domain Admins&#8221; Then\n        Wscript.Echo objUser.Name\n    End If\nNext\n<\/PRE>\n<P>Now replace the AND with OR and see what happens.<\/P>\n<P>See? Every now and then we actually <I>do<\/I> know what we\u2019re talking about!<\/P>\n<P>So what happens if an account meets the criteria; for example, the account kenmyer is not equal to Administrator <I>and<\/I> it is not equal to Domain Admins. In that case, we simply call the <B>Remove<\/B> method, passing it the AdsPath of the account in question; that removes the account from the group:<\/P><PRE class=\"codeSample\">objGroup.Remove(objUser.AdsPath)\n<\/PRE>\n<P>We then repeat the process with the other group members. When we\u2019re done the local Administrators group should have only two members: Administrator and Domain Admins.<\/P>\n<P>Like we said, make sure this is what you want before you use this script. But how about this: be careful when you use this script and, in return, we\u2019ll let you run with scissors. Deal?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I delete everyone except the Administrator and the Domain Admins group from the local Administrators group?&#8212; JS Hey, JS. You know, when you become a doctor you have to take the Hippocratic Oath, which famously begins, \u201cFirst, do no harm.\u201d When you become a Scripting Guy you have to take [&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-68353","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 delete everyone except the Administrator and the Domain Admins group from the local Administrators group?&#8212; JS Hey, JS. You know, when you become a doctor you have to take the Hippocratic Oath, which famously begins, \u201cFirst, do no harm.\u201d When you become a Scripting Guy you have to take [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68353","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=68353"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68353\/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=68353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}