Powershell: Adding users to group called "test" if they are in an OU starting with name "RBCO"

I am looking for a Powershell script that will add users to a group called test, if the user is an OU starting with the name "RBCO"
extsupportAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Will SzymkowskiSenior Solution ArchitectCommented:
Try the following...
import-module activedirectory
$Group = "test"
$Search = Get-ADOrganizationalUnit -Filter 'name -like "RBCO*"'
$Users = Get-ADUser -Filter * -SearchBase $Search | select name, samaccountname, DistinguishedName

ForEach ($user in $users)
            {

                Add-ADGroupMember -Identity $group -Members $user

}

Open in new window


Will.
extsupportAuthor Commented:
Not working yet. Thank you


Get-ADUser : Cannot convert 'System.Object[]' to the type 'System.String' requi
red by parameter 'SearchBase'. Specified method is not supported.
At line:1 char:42
+ $Users = Get-ADUser -Filter * -SearchBase <<<<  $Search | select name, samacc
ountname, DistinguishedName
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBind
   ingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.ActiveDirectory.
   Management.Commands.GetADUser


Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argume
nt is null or empty. Supply an argument that is not null or empty and then try
the command again.
At line:3 char:60
+                 Add-ADGroupMember -Identity $group -Members <<<<  $user
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterB
   indingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.Activ
   eDirectory.Management.Commands.AddADGroupMember
Will SzymkowskiSenior Solution ArchitectCommented:
I have just made a slight modification to the script and i have tested this in my lab and it works without any issues.

import-module activedirectory
$Group = "test"
$Search = Get-ADOrganizationalUnit -Filter 'name -like "RBCO*"'
$Users = Get-ADUser -Filter * -SearchBase $Search

ForEach ($user in $users)
            {
                $user.sAMAccountName

                Add-ADGroupMember -Identity $group -Members $user.sAMAccountName

}

Open in new window


Note: make sure that if you are running this script from the powershell console you need to copy and paste the ENTIRE script into NOTEPAD and save the file as filename.ps1. You will then need to open the powershell console and navigate to the location where you saved the script and type .\filename.ps1.

Or you can simple copy the entire script and paste it into the Powershell ISE and run it there.

Will.
Virus Depot: Cyber Crime Becomes Big Business

The rising threat of malware-as-a-service is not one to be overlooked. Malware-as-a-service is growing and easily purchased from a full-service cyber-criminal store in a “Virus Depot” fashion. View our webinar recording to learn how to best defend against these attacks!

Will SzymkowskiSenior Solution ArchitectCommented:
Ok modified it and it should work now... Let me know if you run into any issues.
Import-module activedirectory
$Group = "test"
$Search = Get-ADOrganizationalUnit -Filter 'name -like "RBCO*"'

    ForEach ($OU in $Search) 
            {

                $OU.DistinguishedName

                $User = Get-ADUser -Filter * -SearchBase $OU.DistinguishedName
                $User.sAMAccountName
                $User | Foreach { Add-ADGroupMember -Identity $Group -Members $User.sAMAccountName }
        
}

Open in new window


Will.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
extsupportAuthor Commented:
Will is a very hard worker and is willing to help and assist anytime.
extsupportAuthor Commented:
Is it easy to do a script that removes users that are no longer in the RBCD OUs?
Will SzymkowskiSenior Solution ArchitectCommented:
Should be easy enough.

Open a new question and send me the link I will try and assist.

Will.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.