I found a Fourm that had a post on it by Richard Mueller that had a script he posted to help setup automation of adding users to a group with a PowerShell Script. The script works great, but I want to add more $OU's to it but when you do that, it only pulls the users from the last OU. I am wondering what commands I need in order to add more OU's?
Here is the script that Richard posted:
----------------------------------------------------
A PowerShell script that ensures that all users in a specified OU are members of a specified group. This also ensures that there are no other members.
# PowerShell script to ensure that all users in a specified OU # are members of a specified group. Also ensure that there are # no members of the group that are not user objects in the OU. # Specify the OU. $OU = [ADSI]"LDAP://ou=West,dc=MyDomain,dc=com"
# Specify the group. $Group = [ADSI]"LDAP://cn=MyGroup,ou=West,dc=MyDomain,dc=com" # Hash table of users in the OU. $List = @{} # Enumerate all objects in the OU. $arrChildren = $OU.Get_Children() ForEach ($Child In $arrChildren) { # Only consider user objects. If ($Child.Class -eq "user") { # Add all users in the OU to the hash table. $List.Add($Child.distinguishedName, $True) # Check if user a member of the group. If ($Group.IsMember($Child.ADsPath) -eq $False) { # Add the user to the group. $Group.Add($Child.ADsPath)"Added " + $Child.distinguishedName } } } # Enumerate all members of the group. ForEach ($Member in $Group.member) { # Check if this member object is a user object in the OU. If ($List.ContainsKey($Member)-eq $False) { # Remove this member from the group. $Group.Remove("LDAP://$Member")"Removed " + $Member } }