Home Blog Active Directory How To Use Powershell to Add Users to Groups from CSV in AD

How To Use Powershell to Add Users to Groups from CSV in AD

0
How To Use Powershell to Add Users to Groups from CSV in AD
powershell add user to group from csv

Introduction

When adding a bulk of users to different Active Directory groups, you should use Powershell.

In this blog post, I provide a solution to use Powershell to add AD users to groups from a CSV file.

Powershell script to AD users to groups

This script adds the users specified in the Users column into the groups specified in the Groups column in the CSV.

Grab the script and an example CSV from the TechNet Gallery.

The script is also available below:

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 $CSV
    }
    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?

Related posts

LEAVE A REPLY

Please enter your comment!
Please enter your name here