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.

PowerShell: Remove Users from AD Group Using a CSV

When someone hands you a spreadsheet of staff who should no longer be in a security group, clicking through Active Directory Users and Computers is slow and error-prone. In 2022 I wrote a short PowerShell script to remove users from an AD group using a CSV of email addresses, and the approach still works today. Below is the original, followed by the safer version I now recommend.

Why remove users from an AD group with a script

Group memberships pile up as people change roles, finish projects or leave. HR or a department head usually sends a list of email addresses, not usernames. So the script needs to read each email, find the matching AD account, and remove it from the group.

Two cmdlets in the ActiveDirectory module do the work: Get-ADUser to find the account and Remove-ADGroupMember to remove it. Microsoft still documents Remove-ADGroupMember for Windows Server 2025, so nothing here has been retired or renamed.

Before you start

  1. Install the ActiveDirectory module. On a domain controller it is already present. On an admin workstation, add the Windows optional feature RSAT: Active Directory Domain Services and Lightweight Directory Services Tools, as described in the ActiveDirectory module documentation. Windows PowerShell 5.1 works, and PowerShell 7 can also load the module on recent Windows versions.
  2. Prepare the CSV with a header called email:
  1. Back up the current membership. This is your undo list if something goes wrong:
Get-ADGroupMember -Identity 'GroupName' |
    Select-Object Name, SamAccountName, DistinguishedName |
    Export-Csv -Path 'C:\Path\to\GroupName-backup.csv' -NoTypeInformation

My original 2022 script

This is the core idea, with one typo corrected. The published version showed $\_ instead of $_, which stops it running:

Write-Host "Removing users from an AD group."
Import-Csv "Path\to\file\users.csv" | ForEach-Object {
    $user = $_.email
    $aduserinfo = Get-ADUser -Filter "EmailAddress -eq '$user'"
    Remove-ADGroupMember -Identity "GroupName" -Members $aduserinfo -Confirm:$false
    Write-Host "User $user successfully removed from the AD group"
}

It reads each row and matches the email (EmailAddress maps to the mail attribute). It then removes the user without prompting.

Update 2026: a safer version

The original works, but it has gaps I would not accept in production now:

  • No check that exactly one user matched. If nobody matches, the removal fails. If two accounts share an email, both are removed.
  • The success message always prints, even when removal failed.
  • Building the filter from text is fragile. An email with an apostrophe breaks it. Passing the variable directly avoids that.
  • No dry run and no log. -Confirm:$false removes people silently, with no record of what happened.

Here is the version I recommend. It starts in dry-run mode, using -WhatIf behind a simple switch:

#Requires -Modules ActiveDirectory
$GroupName = 'GroupName'
$CsvPath   = 'C:\Path\to\users.csv'
$LogPath   = "C:\Path\to\removal-log-$(Get-Date -Format yyyyMMdd-HHmmss).csv"
$DryRun    = $true   # set to $false after checking the dry-run output

$group = Get-ADGroup -Identity $GroupName -ErrorAction Stop

$results = foreach ($row in Import-Csv -Path $CsvPath) {
    $email = "$($row.email)".Trim()
    if (-not $email) { continue }

    $users = @(Get-ADUser -Filter 'mail -eq $email')
    if ($users.Count -ne 1) {
        [pscustomobject]@{ Email = $email; Status = "Skipped: $($users.Count) matches" }
        continue
    }

    try {
        Remove-ADGroupMember -Identity $group -Members $users[0] -Confirm:$false -WhatIf:$DryRun -ErrorAction Stop
        $status = if ($DryRun) { 'Would remove' } else { 'Removed' }
        [pscustomobject]@{ Email = $email; Status = $status }
    }
    catch {
        [pscustomobject]@{ Email = $email; Status = "Error: $($_.Exception.Message)" }
    }
}

$results | Export-Csv -Path $LogPath -NoTypeInformation
$results | Format-Table -AutoSize

The steps to run it:

  1. Set $GroupName, $CsvPath and $LogPath.
  2. Run it as is. With $DryRun = $true, -WhatIf shows what would be removed, nothing changes, and the log marks each row "Would remove".
  3. Read the output and investigate every "Skipped" row. Zero matches usually means a typo or an old address. More than one means duplicate mail values that need cleaning.
  4. Set $DryRun = $false and run it again for real.
  5. Keep the log file with your change record.

How to check it worked

Confirm the users are gone from the group:

$emails = (Import-Csv 'C:\Path\to\users.csv').email
Get-ADGroupMember -Identity 'GroupName' |
    Where-Object { $_.objectClass -eq 'user' } |
    Get-ADUser -Properties mail |
    Where-Object { $emails -contains $_.mail } |
    Select-Object Name, mail

No output means nobody from the list remains. Then compare the member count against your backup file to make sure only the intended accounts were removed.

Cautions

  • Back up first, always. If the list was wrong, the membership export above lets you restore people with Add-ADGroupMember.
  • Use least privilege. Run the script with an account that has only been delegated rights to manage that group's membership, not a Domain Admin account. If the account lacks permission, the cmdlet stops with an error, which the script records.
  • Mind group types. These cmdlets are for on-premises Active Directory. Groups that exist only in Entra ID or Microsoft 365 need Microsoft Graph PowerShell instead. Groups synced from on-premises AD must still be changed on-premises.
  • Keep the evidence. The timestamped log, plus the request that triggered the change, is the kind of access-review record an ISO/IEC 27001 auditor may ask for.
  • Expect replication delay. In larger domains, other domain controllers may show the change a little later.

If your team would rather hand routine directory work and security reviews to someone else, my company, Standing, provides managed IT and information security services in Iraq and the Kurdistan Region.

The takeaway: the 2022 idea is still sound, but never run a bulk removal blind. Back up the group, dry-run first, act only on exact single matches, and keep the log.

Sources & further reading

  1. Microsoft Learn – Remove-ADGroupMember (Windows Server 2025)learn.microsoft.com
  2. Microsoft Learn – ActiveDirectory modulelearn.microsoft.com
  3. Microsoft Q&A – Remove users from AD grouplearn.microsoft.com
  4. 4sysops – How to install the PowerShell 7 Active Directory module4sysops.com
  5. Microsoft Learn – Microsoft Graph PowerShell migration stepslearn.microsoft.com

📝 First written from my own work in 2022; updated and expanded in November 2026 with AI assistance, keeping my original fix. Spotted something wrong? Tell me.

Akam Omer
Written by

Akam Omer

IT leader, engineer and security specialist from Erbil with 20+ years across higher education, telecoms, healthcare and government. Founder of BulkSMSIraq, VerifyWay and Kurd.ai.