Automatically add users to AD security group from a multiple OUs of Active Directory
Please schedule a Task and it will work for you. If you would like to add more OUs then you need to create a variables like below.
#Variables
$TargetGroup = “Testing“
$TargetOU = “OU=Test,OU=UAT,OU=Corporate Group Users,OU=Corporate Group,DC=bbntech,DC=com“
$TargetOU1 ="OU=NO GPOs,OU=Corporate Group Users,OU=Corporate Group,DC=bbntech,DC=com"
#Target user query
$UserAccounts = Get-ADUser -Filter * | ?{($_.DistinguishedName -like “*$TargetOU*”) -or ($_.DistinguishedName -like “*$TargetOU1*”) -and $_.Enabled -eq “True”}
ForEach($User in $UserAccounts)
{
$UsersName = $User.Name
#Check for group membership
$Membership = Get-ADGroup $TargetGroup | Get-ADGroupMember | ?{$_.Name -eq $UsersName}
if(!$Membership)
{
“Adding $UsersName to $TargetGroup”
Get-ADGroup $TargetGroup | Add-ADGroupMember -Members $User -Verbose
}
}
Comments