In this blog, I will show you how to add users to Active Directory security groups using PowerShell, Adding user to any group in an active directory is a very simple task. Either you can right-click on the user and add it to the group or directly you can go on the group and add members.

Adding a single user in a group with PowerShell:

Add-ADGroupMember -Identity infra_ops -Members eng1
On the Above Poweshell command, infra_ops is the group name and eng1 is the user name which we are adding.

Adding Multiple Users in a group (infra_ops)

Create a CSV file with all users list and save it in the same location from where this will be called in a variable, In the below example this is called in $user.

Run the below PowerShell command

Import-Module ActiveDirectory
# Import the data from CSV file and assign it to variable
$Users = Import-Csv "C:\ad_data\infra_ops.csv"
# Specify target group where the users will be added to
$Group = "infra_ops" 
foreach ($User in $Users) {
    # Retrieve UPN
    $UPN = $User.UserPrincipalName
    # Retrieve UPN related SamAccountName
    $ADUser = Get-ADUser -Filter "UserPrincipalName -eq '$UPN'" | Select-Object SamAccountName
    # User from CSV not in AD
    if ($ADUser -eq $null) {
        Write-Host "$UPN does not exist in AD" -ForegroundColor Red
    }
    else {
        # Retrieve AD user group membership
        $ExistingGroups = Get-ADPrincipalGroupMembership $ADUser.SamAccountName | Select-Object Name

        # User already member of group
        if ($ExistingGroups.Name -eq $Group) {
            Write-Host "$UPN already exists in $Group" -ForeGroundColor Yellow
        }
        else {
            # Add user to group
            Add-ADGroupMember -Identity $Group -Members $ADUser.SamAccountName -WhatIf
            Write-Host "Added $UPN to $Group" -ForeGroundColor Green
        }
    }
}

By amit_g

Welcome to my IT Infra Blog! My name is Amit Kumar, and I am an IT infrastructure expert with over 11 years of experience in the field. Throughout my career, I have worked with a wide variety of systems and technologies, from network infrastructure and cloud computing to hardware and software development. On this blog, I aim to share my knowledge, insights, and opinions on all things related to IT infrastructure. From industry trends and best practices to tips and tricks for managing complex systems, my goal is to provide valuable information that will help IT professionals and enthusiasts alike. Whether you are a seasoned IT veteran or just getting started in the field, I hope you will find my blog to be a valuable resource. In addition to sharing my own thoughts and ideas, I also welcome feedback, comments, and questions from my readers. I believe that a collaborative approach is the best way to advance the field of IT infrastructure and I look forward to hearing from you. Thank you for visiting my blog, and I hope you will continue to follow along as I explore the fascinating world of IT infrastructure. Sincerely, Amit Kumar

2 thoughts on “Adding Multiple Users in Active Directory Group”

Leave a Reply

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