PowerShell: Remove Users from AD Group Using a CSV
A PowerShell script to remove users from an AD group using a CSV of email addresses, updated for 2026 with dry runs, error handling and an audit log.
Fix the PHP ldap_search() 'Sizelimit exceeded' warning against Active Directory: page results in code, or raise MaxPageSize with ntdsutil safely.

If your PHP application searches Active Directory and more than 1,000 objects match, you hit the directory's LDAP query size limit. You will see this warning: Warning: ldap_search(): Partial search results returned: Sizelimit exceeded. Your code receives the first 1,000 entries and misses the rest unless you handle it.
In 2021 I fixed this by raising the LDAP query size limit on the domain controller. That method still works, and I keep it below. But the better fix today is in the PHP code itself, so I start there.
Active Directory has a setting called MaxPageSize, which controls how many objects a single LDAP search can return. The default is still 1,000, and it applies however large or small each object is.
The setting lives in the Default Query Policy, which applies across the directory, not on one server. Microsoft's guidance is clear: a client that expects more results than the limit must use the paged search control (Microsoft Learn: view and set LDAP policy using ntdsutil). In other words, the server limit is working as intended; the client should ask for results page by page.
Older tutorials use ldap_control_paged_result() and ldap_control_paged_result_response(). These were deprecated in PHP 7.4 and removed in PHP 8.0. The PHP manual now points to the controls parameter of ldap_search() instead (PHP manual: ldap_control_paged_result).
With paging, the server limit can stay at 1,000 and you still get every object:
// Example only: paged search against Active Directory (PHP 7.3+)
$ldap = ldap_connect('ldaps://dc1.example.local');
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
if (!ldap_bind($ldap, getenv('LDAP_BIND_USER'), getenv('LDAP_BIND_PASSWORD'))) {
throw new RuntimeException(ldap_error($ldap));
}
$cookie = '';
do {
$result = ldap_search($ldap, $baseDn, $filter, ['cn', 'mail'], 0, -1, -1, LDAP_DEREF_NEVER,
[['oid' => LDAP_CONTROL_PAGEDRESULTS, 'value' => ['size' => 500, 'cookie' => $cookie]]]);
if ($result === false) {
throw new RuntimeException(ldap_error($ldap));
}
ldap_parse_result($ldap, $result, $errcode, $matcheddn, $errmsg, $referrals, $controls);
$entries = ldap_get_entries($ldap, $result);
// process $entries ... ($entries['count'] is the number on this page)
$cookie = $controls[LDAP_CONTROL_PAGEDRESULTS]['value']['cookie'] ?? '';
} while (!empty($cookie));
A page size of 500 keeps each response well under the limit. The loop ends when the server stops returning a cookie. If you use a framework or LDAP library rather than the raw functions, check that your version pages through server controls on PHP 8, and run a supported PHP version (PHP supported versions).
If you cannot change the application, you can raise the limit on the directory. Use a modest value, and read the cautions first.
ntdsutil and press Enter.ntdsutil: prompt, type ldap policies.ldap policy: prompt, type connections.server connections: prompt, type connect to server followed by the FQDN of your domain controller, for example connect to server dc1.example.local.q to return to the ldap policy: prompt.set MaxPageSize to 5000.show values to review the change before committing it.commit changes, then q twice to exit.The full session looks like this:
ntdsutil
ldap policies
connections
connect to server dc1.example.local
q
set MaxPageSize to 5000
show values
commit changes
q
q
In my original note, show values came after the commit. I now run it before, because at that point it shows the current and proposed values side by side, such as 1000(5000). You can spot a typo before it goes live.
ntdsutil, go back into ldap policies, connect, and type show values. MaxPageSize should show the new value with no pending change in brackets.Sizelimit exceeded warning should be gone.show values before you change anything, and follow your change-control process. Make sure you have a current backup of your domain controllers before modifying directory settings.member attribute is governed by a different range limit, so raising MaxPageSize will not return all members. That needs ranged retrieval in the client.| Situation | What I recommend |
|---|---|
| You control the PHP code | Page results with the controls parameter; leave MaxPageSize at 1,000 |
| Legacy app you cannot change | Raise MaxPageSize modestly with ntdsutil, after a backup |
Code uses ldap_control_paged_result() |
Rewrite it; it does not exist in PHP 8.0 and later |
| Missing members of a large group | Use ranged retrieval; MaxPageSize will not help |
The ntdsutil change got me out of trouble in 2021, and it still works. But the lasting fix is to make your PHP code ask for results in pages. It scales, it leaves the directory at its defaults, and it keeps working as your directory grows.
📝 First written from my own work in 2021; updated and expanded in November 2026 with AI assistance, keeping my original fix. Spotted something wrong? Tell me.