Link to home
Start Free TrialLog in
Avatar of extsupport
extsupportFlag for United States of America

asked on

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"
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

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.
Avatar of extsupport

ASKER

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
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.
ASKER CERTIFIED SOLUTION
Avatar of Will Szymkowski
Will Szymkowski
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Will is a very hard worker and is willing to help and assist anytime.
Is it easy to do a script that removes users that are no longer in the RBCD OUs?
Should be easy enough.

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

Will.