Add AD Group Members using PowerShell Script

In this article, I am going to write powershell script samples to add members to Active Directory group and add AD group members from CSV file. You can add group members by using the Active Directory powershell cmdlet Add-ADGroupMember.

Add-ADGroupMember [-Identity] <ADGroup> [-Members] <ADPrincipal[]>

Identity – The Identity parameter specifies the Active Directory group that receives the new members. You can identify a group by its distinguished name (DN), GUID, SID or SamAccountName.

Members – The Members parameter specifies the new members to add to a group. You can identify a new member by its distinguished name (DN), GUID, SID or SamAccountName.

Add Active Directory Group Members

Add user accounts to AD Group by samAccountName:

Import-Module ActiveDirectory
Add-ADGroupMember "Domain Admins" "MorganTest1,MorganTest2";

Add AD Group members by distinguished name (DN):

Import-Module ActiveDirectory
Add-ADGroupMember "Domain Admins" "CN=MorganTest1,OU=TestOU,DC=TestDomain,DC=local";

Add Members to AD Group from CSV file

   1. Consider the CSV file Users.csv which contains set of Active Directory users to add as members to AD Group with the attribute samAccountName.

Disable Active Directory User Account using Powershell Script

   2. Copy the below Powershell script and paste in Notepad file.
   3. Change the Users.csv file path with your own csv file path.
   4. SaveAs the Notepad file with the extension .ps1 like Import-AD-Group-Members-From-CSV.ps1

Powershell script file: Download Import-AD-Group-Members-From-CSV.ps1

Import-Module ActiveDirectory
  $adGroup = "Powershell Admins"
Import-Csv "C:\ScriptsUsers.csv" | ForEach-Object {
 $samAccountName = $_."samAccountName" 
 Add-ADGroupMember $adGroup $samAccountName;
 Write-Host "- "$samAccountName" added to "$adGroup
}

   5. Now run the file Import-AD-Group-Members-From-CSV.ps1 from Powershell to import Bulk Active Directory users from CSV and add as member to AD Group.

PS C:Scripts>  .Import-AD-Group-Members-From-CSV.ps1
Add Members to AD Group by Importing Members From CSV using Powershell Script

Note: I have placed script file in the location C:Scripts, if you placed in any other location, you can navigate to that path using CD path command (like cd “C:\Downloads”).


Advertisement

Leave a Comment