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
}
}
}
User does not exist in ad error is coming
Can you please share the error screenshot and csv file and powershell script on amitceh@gmail.com . Meanwhile you can watch the same on
https://youtu.be/VV82LC3umFc