Bulk add users to a group from CSV
Documentation: Microsoft Learn
Import-Csv reads the file into objects with one property per column, and ForEach-Object calls Add-ADGroupMember once per row.
The script
Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
$Group = 'Sales' # target group; change to your group name
Import-Csv .\users.csv | ForEach-Object { Add-ADGroupMember -Identity $Group -Members (Get-ADUser $_.SamAccountName -ErrorAction Stop) -ErrorAction Stop }- Stage 1$Group
Positional values or no named parameters - Stage 2ForEach-Object
Positional values or no named parameters
How it works
Import-Csv reads the file into objects with one property per column, and ForEach-Object calls Add-ADGroupMember once per row. Each row's SamAccountName is resolved through Get-ADUser with -EA Stop first, so a name that does not exist raises a terminating error rather than quietly adding nothing.
Adapting it
Collect the resolved users into an array and make one Add-ADGroupMember call with -Members taking the whole array, which turns forty directory round trips into a single operation.
Requirements
ActiveDirectory module plus write access to the target group.
If the result is unexpected
Bulk group additions fail for some CSV rows or target the wrong accounts.
Header names, identity formats and group names may not match the command parameters; partial completion is possible.
- Validate the CSV columns and resolve one user and group before reviewing the proposed batch.
Validate the result
Each input row has an individual result and successful additions are confirmed in membership.
Keep the check reliable
Validate the input contract and make repeat runs check existing membership first.