This PowerShell script imports the Active Directory module, reads a list of users and groups from a CSV file, and then loops through each user in the CSV. For each user, it checks if the user is enabled in Active Directory using the Get-ADUser cmdlet. If the user is enabled, it then checks if the user is already a member of the group specified in the CSV using the Get-ADGroupMember cmdlet. If the user is already a member of the group, it outputs a message and skips the user. If the user is not already a member of the group, it adds the user to the group using the Add-ADGroupMember cmdlet. If the user is disabled in Active Directory, it outputs a message and skips the user. The script uses Write-Host to output messages to the console.
Note that this script assumes that the CSV file contains a column for the user's username named "User", and a column for the group name named "Group". The script also assumes that the CSV file is located at "c:\ad_script\UserList.csv".
Script:
Import active directory module for running AD cmdlets
Import-module ActiveDirectory
#Store the data from UserList.csv in the $List variable
$List = Import-CSV c:\ad_script\UserList.csv
#Loop through user in the CSV
ForEach ($User in $List)
{
Check if user is enabled
$ADUser = Get-ADUser $User.User -Properties Enabled
If ($ADUser.Enabled -eq $true) {
Check if user is already a member of the group
If (Get-ADGroupMember -Identity $User.Group -Recursive | Where-Object {$_.SamAccountName -eq $User.User}) {
User is already a member of the group, output a message and skip them
Write-Host "User $($User.User) is already a member of group $($User.Group), skipping." -ForegroundColor Yellow
}
Else {
User is not already a member of the group, add them to the group
Add-ADGroupMember $User.Group -Members $User.User -ErrorAction Stop -Verbose
}
}
Else {
User is disabled, skip them
Write-Host "User $($User.User) is disabled, skipping." -ForegroundColor Red
}
}
Watch video Bulk Add Active Directory Group Members with PowerShell online without registration, duration hours minute second in high quality. This video was added by user InfoAlias 11 February 2023, don't forget to share it with your friends and acquaintances, it has been viewed on our site 2,124 once and liked it 20 people.