[code],  Hello IT,  Windows

Delete all users

Windows CMD tool to delete all users

This is a small windows command line tool I made to delete all users (local only) that are not part of the administrator group.

At regular basis, I received demo stations back from exhibitions and training sessions. Those demo stations needed to be cleaned up for the next exhibition or training, and manually deleting the users was a boring job that took too much time, in my opinion. So I developed this little tool in my spare time to help me safe time at work.

Delete all users

You can download this tool from Github page

using System;
using System.DirectoryServices;

namespace DelAllUsers
{
    class Program
    {

        static void Main(string[] args)
        {
            int delcount = 0;
            Boolean ignore = false;
            Boolean sys = false;
            string name = null;
            string[] ignoreList = { "guest","wdagutilityaccount" };
            string sPath = "WinNT://" + Environment.MachineName + ",computer";
            DirectoryEntry localDirectory = new DirectoryEntry(sPath);
            using (var computerEntry = localDirectory)
                foreach (DirectoryEntry childEntry in computerEntry.Children)
                    if (childEntry.SchemaClassName == "User")
                    {
                        name = childEntry.Name;
                        ignore = false;
                        sys = false;
                        for (int i = 0; i<ignoreList.Length; i++)
                        {
                            if (name.ToLower().Equals(ignoreList[i])){
                                ignore = true;
                                i = ignoreList.Length + 5;
                             }
                        }
                        if (!ignore)
                        {
                            object obGroups = childEntry.Invoke("Groups");
                            foreach (object ob in (System.Collections.IEnumerable)obGroups)
                            {
                                DirectoryEntry obGpEntry = new DirectoryEntry(ob);
                                if (obGpEntry.Name.ToLower().Equals("administrators") || obGpEntry.Name.ToLower().Equals("system managed accounts group"))
                                {
                                    sys = true;
                                }
                                obGpEntry.Close();
                            }

                            if (!sys)
                            {
                                try
                                {
                                    DirectoryEntries users = localDirectory.Children;
                                    DirectoryEntry user = users.Find(name);
                                    users.Remove(user);
                                    delcount++;
                                }
                                catch(Exception err)
                                { Console.WriteLine(err.ToString()); }
                            }
                        }
                    }
            Console.WriteLine("\t " + delcount + "Users Deleted");
        }
    }
}

signature

Laat een antwoord achter

Het e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *