Solved

Batch script to delete all users from domain admin group but administrators

Posted on 2014-11-19
3
781 Views
Last Modified: 2015-01-14
Hi Guys,

What I wanna achieve is same as mentioned here - http://blogs.technet.com/b/heyscriptingguy/archive/2005/12/12/how-can-i-delete-everyone-except-the-administrator-and-the-domain-admins-group-from-the-local-administrators-group.aspx but how do I do it from a batch script.

I know the command to add administrators is
net localgroup administrators /add "domain\user", but I want a script that will delete all users from administrators group, even ones that are unknown and we dont want to use Group Policy for this.
0
Comment
Question by:manav08
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Accepted Solution

by:
NVIT earned 500 total points
ID: 40453531
Put these into a .bat file.

Warning: I recommend testing this on a test group first. When you know it works, do it on the Domain Admins group.

Note: To actually run it, remove the ECHO in front of net group /delete

@echo off
if exist DomainAdmins.txt del DomainAdmins.txt
for /F "tokens=1,2,3,4,5 delims==, " %C in ('dsquery group -name "Domain Admins" ^| dsget group -members ^| dsget user -samid') do echo %C>>DomainAdmins.txt
echo Deleting members of Domain Admins...

for /F %%A in (DomainAdmins.txt) do (call :DoIt %%A)
goto :EOF

:DoIt
if %1 equ samid goto :EOF
if %1 equ dsget goto :EOF
ECHO net group /delete /domain "%1"

Open in new window

0
 
LVL 24

Expert Comment

by:NVIT
ID: 40549927
Hi manav08,
Did the solution help you? If so, I'd appreciate you closing the question appropriately.
0
 
LVL 11

Author Closing Comment

by:manav08
ID: 40550631
Apologies for my laziness in closing this question.
I havent tested but believe this is solution we were after
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question