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.
A PowerShell script to enable inheritance for bulk AD users in an OU, with a safer 2026 version: dry run, ACL backup, AdminSDHolder checks and rollback.

You delegate a permission on an OU, such as password resets for the helpdesk, and it works for most accounts but not all. When you open the Security tab on one of the failing accounts, you find that inheritance is switched off. Fixing one account takes a few clicks. When you need to enable inheritance for bulk AD users, such as hundreds of student accounts, you need a script.
In 2020 I used a short PowerShell script to do exactly that for every account under a specific OU. The idea still works. Below is my original script, followed by a safer version I would use today.
When inheritance is disabled, an account's ACL contains only explicit entries. Anything delegated on the parent OU never reaches it. In my experience this happens after migrations, after accounts are copied from old templates, after manual changes, or because the account belongs to a protected group. That last case is intentional, and I cover it below.
adminCount=1. The AdminSDHolder process deliberately switches their inheritance off again (Microsoft: protected accounts and groups). Leave those accounts alone.-searchbase value to your own OU. The example below uses a placeholder domain.$ADusers = Get-ADUser -ldapfilter "(objectclass=user)" -searchbase "OU=Students,DC=example,DC=com"
ForEach($user in $ADusers)
{
# Binding the users to DS
$ou = [ADSI]("LDAP://" + $user)
$sec = $ou.psbase.objectSecurity
if ($sec.get_AreAccessRulesProtected())
{
$isProtected = $false ## allows inheritance
$preserveInheritance = $true ## preserve inherited rules
$sec.SetAccessRuleProtection($isProtected, $preserveInheritance)
$ou.psbase.commitchanges()
Write-Host "$user is now inheriting permissions";
}
else
{
Write-Host "$user Inheritable Permission already set"
}
}
The key line is SetAccessRuleProtection($false, $true). The first argument turns protection off, which enables inheritance. The second keeps the explicit rules the account already has (.NET documentation).
The original still runs, but I would change a few things:
Get-ADUser already returns only user objects, so -Filter * is simpler. Be careful reusing (objectClass=user) elsewhere, because on its own it also matches computer objects.DistinguishedName explicitly through the AD: drive instead of building an ADSI path from a string.adminCount=1 accounts and report them.#Requires -Modules ActiveDirectory
# Example only: test in a lab first. Set $DryRun = $false to apply changes.
Import-Module ActiveDirectory
$SearchBase = 'OU=Students,DC=example,DC=com'
$DryRun = $true
$Backup = "C:\Temp\acl-backup-$(Get-Date -Format yyyyMMdd-HHmm).csv"
Get-ADUser -Filter * -SearchBase $SearchBase -Properties adminCount | ForEach-Object {
$path = "AD:\$($_.DistinguishedName)"
$acl = Get-Acl -Path $path
if (-not $acl.AreAccessRulesProtected) { Write-Host "OK $($_.SamAccountName)"; return }
if ($_.adminCount -eq 1) { Write-Warning "SKIP $($_.SamAccountName) (AdminSDHolder-protected)"; return }
[pscustomobject]@{ DN = $_.DistinguishedName; Sddl = $acl.Sddl } |
Export-Csv -Path $Backup -Append -NoTypeInformation
$acl.SetAccessRuleProtection($false, $true) # enable inheritance, keep existing rules
Set-Acl -Path $path -AclObject $acl -WhatIf:$DryRun
Write-Host ("{0} {1}" -f ($(if ($DryRun) {'WOULD FIX'} else {'FIXED'})), $_.SamAccountName)
}
Make sure the C:\Temp folder exists before you run it, or change the backup path. Otherwise the export will fail.
Run it once with $DryRun = $true and read the output. If the list of accounts looks right, set it to $false and run it again. Set-Acl supports -WhatIf, which is what makes the dry run possible (Set-Acl documentation).
One thing to watch is that adminCount is not always cleared when someone leaves a privileged group. If the script skips accounts that you know are no longer admins, review those few by hand rather than changing the script.
This lists any accounts in the OU that still have inheritance disabled. Afterwards, only the skipped protected accounts should remain:
Get-ADUser -Filter * -SearchBase 'OU=Students,DC=example,DC=com' |
Where-Object { (Get-Acl "AD:\$($_.DistinguishedName)").AreAccessRulesProtected } |
Select-Object SamAccountName
To check one account in the GUI, open Active Directory Users and Computers and turn on View → Advanced Features. Then open the account and go to Properties → Security → Advanced. The Enable inheritance button should no longer be offered.
If something goes wrong, restore each account's ACL from the CSV backup:
# Example only: restores ACLs saved by the script above
Import-Csv 'C:\Temp\acl-backup-YYYYMMDD-HHMM.csv' | ForEach-Object {
$path = "AD:\$($_.DN)"
$acl = Get-Acl -Path $path
$acl.SetSecurityDescriptorSddlForm($_.Sddl)
Set-Acl -Path $path -AclObject $acl
}
| Step | Done? |
|---|---|
| Tested on a lab or test OU | |
| Parent OU delegations reviewed | |
| Backup folder exists | |
| Dry run output checked | |
| SDDL backup file saved somewhere safe | |
Protected (adminCount=1) accounts left alone |
|
| Verification query shows no unexpected accounts |
The takeaway: enabling inheritance in bulk is a one-line change inside a loop, but treat it like any other permission change. Back up first, do a dry run, and know what the parent OU grants. If you are tidying up delegation across a whole directory and want help, my company Standing provides information security and managed IT services.
📝 First written from my own work in 2020; updated and expanded in November 2026 with AI assistance, keeping my original fix. Spotted something wrong? Tell me.