user10579342
user10579342

Reputation: 15

Add ALL users of a OU to a security group also the ones in a security group

I want to add all users of my OU into a specific security group. I used the command below, which showed me all Users of the OU, but not the ones in a security group.

So how can I fix my command to show me ALL Users in the OU?

GET-ADUser -SearchBase 'OU=OU, DC=DC' -Filter 'enabled -eq $true | % {Add-ADGroupMember 'Security-Group' -Members $_}

Upvotes: 1

Views: 5892

Answers (1)

Bernard Moeskops
Bernard Moeskops

Reputation: 1283

That is because you query all users not groups. If the users inside the security groups are located in a different OU you should also check that OU. Or do something like:

$users = Get-ADUser -SearchBase 'OU=OU, DC=DC' -Filter {(enabled -eq $true)}
foreach($user in $users){
    Add-ADGroupMember 'security-group' -Members $user
}
$groups = Get-ADGroup -Filter * -SearchBase 'OU=OU, DC=DC'
foreach($group in $groups){
    $members = Get-ADGroupMember $group
    foreach($member in $members){
        Add-ADGroupMember 'security-group' -Members $member
    }
}

With the above script, everybody will be added to the security group including users not in that OU but inside a group in that OU.

Hope this helps!

Upvotes: 1

Related Questions