PowerShell: Add All Users in an Active Directory OU to a Group

This handy script below will list all the user objects in an Active Directory organizational unit, then add them to an Active Directory group. You need to have permissions that allow you to add members to the group in order to run this successfully.

Replace the variable information below with your specific domain information.

################################################################################
#                                                                              #
# List all users in OU                                                         #
# Add these users to a group                                                   #
#                                                                              #
################################################################################

$GroupName = “GroupToAddMembersTo”
$OUUsers = Get-ADUser -Filter * -SearchBase “ou=YourOU,dc=YourDomain,dc=YourDomain” | Select-Object samAccountName

foreach ($OUUser in $OUUsers)
{
Add-ADGroupMember -Identity $GroupName -Members $OUUsers.samAccountName
}

Comments are closed.