skip to Main Content

Add users to group with PowerShell

How to bulk add users to AD security group from CSV file with PowerShell? You can select multiple users in AD and add them to the group, but what if you have users all over the place in different OUs? This is an excellent way to automate the task with PowerShell. In this article, you will learn how to bulk add users to a group with PowerShell script.

Information

You need to add a list of users in CSV file to a security group. The list is populated with the UserPrincipalName attribute.

There are two options to accomplish the task:

  • Manually search for the users in Active Directory Users and Computers, and add them to the security group. If you have a long list, this is time-consuming. Not only that, it’s possible that you miss a user from the list.
  • Automate the search with PowerShell, and add the users to the security group. It will take less time, and you will not miss any users.

PowerShell is great for automation, and that’s what we recommend using.

Check security group

Create a security group if you don’t have one. In this example, we have the security group Pilot. The members section is empty.

Add users to group with PowerShell before

Read more: List all users in a Security Group through PowerShell »

Check CSV file with Import-Csv cmdlet

Check the CSV file and that you use the correct header. In our case, it’s the CSV file Users.csv and the header UserPrincipalName.

Add users to group with PowerShell CSV file UPN
Check that there are no empty spaces behind each line. If so, you will get errors, and the script will fail to remove the users.

An excellent way is to add quotation marks to surround the field.

Add users to group with PowerShell CSV file UPN quotation marks

Place the CSV file in C:\Temp folder. Create a temp folder if you don’t have one.

Add users to group with PowerShell CSV file

Run Windows PowerShell as administrator. Make sure that PowerShell can read the file, run Import-Csv cmdlet.

PS C:\> Import-Csv C:\Temp\Users.csv

UserPrincipalName
-----------------
Amanda.Morgan@exoip.com
Max.Fraser@exoip.com
Piers.Bower@exoip.com
Kylie.Davidson@exoip.com
Richard.Grant@exoip.com  
Boris.Campbell@exoip.com
Nicholas.Murray@exoip.com
Leonard.Clark@exoip.com
Ruth.Dickens@exoip.com
Jonathan.Fisher@exoip.com
Grace.Rees@exoip.com
Patrick.Mors@exoip.com
John.Maverick@exoip.com
Ali.Tajran@exoip.com
Alysia.Maverick@exoip.com
Mohammad.Fistak@exoip.com

Add users to group PowerShell script

Download Add-ADUsers.ps1 PowerShell script or copy and paste the below code in Notepad. Give it the name Add-ADUsers.ps1 and place it in the C:\scripts folder. Create a scripts folder if you don’t have one.

# Import AD Module
Import-Module ActiveDirectory

# Import the data from CSV file and assign it to variable
$Users = Import-Csv "C:\Temp\Users.csv"

# Specify target group where the users will be added to
# You can add the distinguishedName of the group. For example: CN=Pilot,OU=Groups,OU=Company,DC=exoip,DC=local
$Group = "Pilot" 

# Retrieve UPN
foreach ($User in $Users) {
    $UPN = $User.UserPrincipalName
    Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" |

    # Add user to group
    ForEach-Object { Add-ADGroupMember -Identity "$Group" -Members "$_" } 
}
  • Line 5: Edit the CSV file name and the path.
  • Line 9: Edit the target OU.

In the next step, we will have a look at the bulk add AD Users PowerShell script.

Bulk add users to group from CSV file

Change the path to the scripts folder. Run the PowerShell script to bulk add AD users to group. Wait till it completes.

PS C:\> cd c:\scripts
PS C:\scripts> .\Add-ADUsers.ps1

Verify security group with added users

When the script finishes, have a look at Active Directory Users and Computers. Go to the security group and validate that you see the users from the CSV file in the members tab. In this case, the Pilot group.

Add users to group with PowerShell after

Everything looks great! Did this help you to bulk add users to security group from CSV file with PowerShell?

Keep reading: Manage Microsoft Office with Group Policy »

Conclusion

In this article, you learned how to add users to group from CSV with PowerShell. Download the Add-ADUsers.ps1 PowerShell script, edit the CSV path and the target group. Run the script and verify that the AD users are added successfully to the group. PowerShell is great for automating the process.

Did you enjoy this article? You may also like Bulk create Office 365 mailboxes in Exchange hybrid. Don’t forget to follow us and share this article.

ALI TAJRAN

ALI TAJRAN

ALI TAJRAN is a passionate IT Architect, IT Consultant, and Microsoft Certified Trainer. He started Information Technology at a very young age, and his goal is to teach and inspire others. Read more »

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top