Query AD without the 1000 limit

Use a custom c# script to download all the ad users.

AD sever has a default setting to limit the AD query result size to 1000. The limit is set on the AD server side, we can't do too much about changing the result size returned per query from the client side.

However, we can use query filter to limit the size of the result per query. As long as we construct a set of filters to fully cover all the users, the final result is correct.

The idea here is to query AD by using employee_id which is a number (e.g. 2000120). For example, we can query AD by checking employee_id = 1*. * is a wildcard. if the size of the result is larger than or equal to 1000, we refine the search into to 10 searches: 10*, 11*, 12*, 13*....19*. By refining the search recursively, we will get to a result set that is smaller than 1000. Then we combine all the smaller searches together.

Typical recursive programming:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.DirectoryServices;

using System.IO;

namespace QueryActiveDirectory_No_1000_Limit

{

    class Program

    {

        public void Main()

        {

            string RootPath = @"LDAP://DC=internal,DC=childcare,DC=com,DC=au";

            DirectoryEntry de = new DirectoryEntry(RootPath, "username", "password");

            List<string> userList = new List<string>();

            getUsers("", de, userList);

            //write the result to a txt file

            StreamWriter sw = new StreamWriter("D:\\users.txt", false);

            foreach (string user in userList)

            { 

                sw.WriteLine(user);

            }

            sw.Close();

        }

        /*

         * The getUsers function here retrieves AD users recursively.

         * If the returned result has >= 1000 users, it refines the search by using a longer prefix.

         * Here the EmployeeID is a number.

         * Can change the filter column to any other number column.

         */

        public void getUsers(string prefix, DirectoryEntry de, List<string> list)

        {

            for (int i = 0; i <= 9; i++)

            {

                string filter = "(&(objectCategory=Person)(objectClass=user)(EmployeeID=" + prefix + i + "*))";

                DirectorySearcher ds = new DirectorySearcher(de, filter);

                SearchResultCollection results = ds.FindAll();

                if (results.Count < 1000 && results.Count > 0)

                    foreach (SearchResult item in results)

                    {

                        list.Add(

                            "\"" + item.Properties["sAMAccountName"][0].ToString() + "\","

                            + "\"" + item.Properties["EmployeeID"][0].ToString() + "\""

                            + "\"" + item.Properties["Mail"][0].ToString() + "\","

                            + "\"" + item.Properties["TelephoneNumber"][0].ToString() + "\","

                            + "\"" + item.Properties["Mobile"][0].ToString() + "\","

                            + "\"" + item.Properties["Name"][0].ToString() + "\""

                        );

                    }

                else if (results.Count <= 0)

                    continue;

                else

                    getUsers(prefix + i, de, list);

            }

        }

    }

}