Solved

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

Posted on 2014-11-19
3
732 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Resolve DNS query failed errors for Exchange
This article explains how to install and use the NTBackup utility that comes with Windows Server.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

733 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