Bulk Add Active Directory Group Members with PowerShell

Опубликовано: 11 Февраль 2023
на канале: InfoAlias
2,124
20

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
}
}


Смотрите видео Bulk Add Active Directory Group Members with PowerShell онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь InfoAlias 11 Февраль 2023, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,124 раз и оно понравилось 20 людям.