Table of Contents
Introduction
If you want to add a user in Active Directory to a group, it might make sense to use Active Directory Users & Computers. But, if you’re going to bulk add thousands of users to a group, it doesn’t make sense.
This is where some Powershell scripting comes in to help!
In this blog post, I provide a script that can be used when adding users to an Active Directory Group using a structured CSV file.
Powershell script to AD users to groups
This Windows Powershell script adds the users specified in the Users column into the groups specified in the Groups column in the CSV.
Note that the script uses the SAM account when adding to the groups with the Add-ADGroupmember CMDLet.
Grab the script and an example CSV from the TechNet Gallery.
The script is also available below. Make sure to modify the CSV file. You can run it both on a Windows Server or Windows 10 machine.
BEGIN{
#Checks if the user is in the administrator group. Warns and stops if the user is not.
If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You are not running this as local administrator. Run it again in an elevated prompt."
Break
}
try {
Import-Module ActiveDirectory
}
catch {
Write-Warning "The Active Directory module was not found"
}
try {
$Users = Import-CSV "path to CSV file"
}
catch {
Write-Warning "The CSV file was not found"
}
}
PROCESS{
foreach($User in $Users){
try{
Add-ADGroupMember $User.Group -Members $User.User -ErrorAction Stop -Verbose
}
catch{
}
}
}
END{
}
Conclusion
Powershell is the way to go if you want to add Active Directory users to groups in bulk.
How do you accomplish this? Do you have any suggestions for updates?
I am unable to locate the CSV file. Can the author share it here.