Solved

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

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Understanding the various editions available is vital when you decide to purchase Windows Server 2012. You need to have a basic understanding of the features and limitations in each edition in order to make a well-informed decision that best suits y…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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…

776 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