Bulk Add Active Directory Group Members with PowerShell

preview_player
Показать описание
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.

Script:

# Import active directory module for running AD cmdlets
Import-module ActiveDirectory

#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
}
}
Рекомендации по теме
Комментарии
Автор

What about I am going to add a Member in the Group but..

Member1 (Domain1) > Group1 (Domain1)

Member2 (Domain2) > Group2 (Domain2)

in 1 PowerShel line..

like my server is Entire Directory and can Access 2 domains.

carlmacaspac