Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 379571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:54:16+00:00 2026-05-12T14:54:16+00:00

I have to list all users (both local system and domain). I tried using

  • 0

I have to list all users (both local system and domain). I tried using WQL but it takes a lot of time for the program to run. Is there any other way to get it from registry? Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:54:17+00:00Added an answer on May 12, 2026 at 2:54 pm
    using System;
    using System.Collections.Generic;
    using System.DirectoryServices;
    namespace ListADUsers.ConsoleApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.Clear();
                IList<String> userList = new List<String>();
                int badEntries = 0;
                string domainName = String.Empty;
                if (args.Length > 0)
                    domainName = args[0];
                else
                {
                    Console.Write("\nPlease enter your Active Directory domain name: ");
                    domainName = Console.ReadLine();
                }
                Console.Write(String.Format("\nAttempting to build user list for {0} ...\n\n", domainName));
                try
                {
                    if (!String.IsNullOrEmpty(domainName))
                    {
                        DirectoryEntry myDirectoryEntry = new DirectoryEntry(String.Format("LDAP://{0}", domainName));
                        DirectorySearcher mySearcher = new DirectorySearcher(myDirectoryEntry);
                        SortOption mySort = new SortOption("sn", SortDirection.Ascending);
                        mySearcher.Filter = ("(objectCategory=person)");
                        mySearcher.Sort = mySort;
                        foreach (SearchResult resEnt in mySearcher.FindAll())
                        {
                            try
                            {
                                if (!String.IsNullOrEmpty(resEnt.Properties["Mail"][0].ToString())
                                    && System.Text.RegularExpressions.Regex.IsMatch(resEnt.Properties["DisplayName"][0].ToString(), " |admin|test|service|system|[$]", System.Text.RegularExpressions.RegexOptions.IgnoreCase)
                                    )
                                    {
                                        int space = resEnt.Properties["DisplayName"][0].ToString().IndexOf(" ");
                                        string formattedName = String.Format("{0}{1}{2}",
                                            resEnt.Properties["DisplayName"][0].ToString().Substring(space).PadRight(25),
                                            resEnt.Properties["DisplayName"][0].ToString().Substring(0, space).PadRight(15),
                                            resEnt.Properties["Mail"][0].ToString()
                                            );
                                        userList.Add(formattedName);
                                    }
                            }
                            catch
                            {
                                badEntries++;
                            }
                        }
                        if (userList.Count > 0)
                        {
                            Console.WriteLine(String.Format("=========== Listing of users in the {0} domain\n", domainName));
                            Console.WriteLine(String.Format("{0}{1}{2}", "Surname".PadRight(25), "First Name".PadRight(15), "Email Address\n"));
                            for (int i = 0; i < userList.Count - 1; i++)
                                Console.WriteLine(userList[i].ToString());
                            Console.WriteLine(String.Format("\n=========== {0} users found in the {1} domain", userList.Count.ToString(), domainName));
                        }
                        else
                            Console.WriteLine(String.Format("\n=========== 0 users found in the {0} domain", userList.Count.ToString()));
                        Console.WriteLine(String.Format("=========== {0} objects could not be read", badEntries.ToString()));
                        Console.WriteLine("=========== End of Listing");
                    }
                    else
                    {
                        Console.WriteLine("Please enter a domain name next time!");
                    }
                }
                catch (Exception ex)
                {
                    // in a production app you wouldn't show the user the exception details
                    Console.Write(String.Format("A critical error occurred.\nDetails: {0}", ex.Message.ToString()));
                }
            }
        }
    }
    

    Download sample application : Sample Application to List AD Users

    Source : Follow Up – List active directory users – This time in C#

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List with all system font names. Here is a part of
This is mainly a performance questions. I have a master list of all users
I have a users list page where the result can be modified by using
I have to list all databases in an exchange 2010 server with their mailboxes.
I have a List contains all float values( lets say List< float > avgDifClosedLoop;
How do I list all tables / columns in my database that have a
I have many string and i put it in a list.All the strings have
So far I have managed to write a query to list all the outgoing
I need some help with my problem. I have created a list of all
I have a list of 400 strings that all end in _GONOGO or _ALLOC.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.