Writing a powershell script to add users from a csv to a group in Active Directory?

Elizabeth N. McClary 20 Reputation points
2024-11-20T19:58:25.32+00:00

We are wanting to use a CSV file to bulk upload users to a certain security group. Some users in the CSV will already be in the group. Is there a good source to help write this powershell script?

Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,726 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,678 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,466 Reputation points
    2024-11-20T20:18:23.3733333+00:00

    Without writing the simple script for you that will do this, the script will follow the steps below.

    Get the distinguishedName of the security group
    for each user in the CSV
        Get the AD User and their memberOf property
        is the security groups' distinguished name in the users' memberOf property?
            do nothing, they're already a member
        if not
            add the user to the security group
    

    If you have a problem with your script, post the script along with an explanation and any error message you receive.

    0 comments No comments

  2. Ian Xue 38,551 Reputation points Microsoft Vendor
    2024-11-21T01:17:14.2233333+00:00

    Hi Elizabeth N. McClary,

    The Import-Csv cmdlet can import the CSV file of the AD users to PowerShell and Add-ADGroupMember adds members to an AD group. Please refer to the help files below.

    https://learn.microsoft.com/en-us/powershell/module/microsoft.Powershell.utility/import-csv

    https://learn.microsoft.com/en-us/powershell/module/activedirectory/add-adgroupmember

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.