Powershell: Adding users to a group by job title

I have this syntax so far. What I am looking to do is, add a user to group by job title

add user to distribution group .RBCO04_Management from OU RBC04 with a title "RBCO MANAGER"

Import-module activedirectory
$Group = ".RBCO04_Management"
$Search = Get-ADOrganizationalUnit -Filter 'name -like "RBCO4"'

    ForEach ($OU in $Search)
            {

                $OU.DistinguishedName

                $User = Get-ADUser -Filter  -properties Title -SearchBase "RBCO MANAGER" $OU.DistinguishedName
                $User.sAMAccountName
                $User | Foreach { Add-ADGroupMember -Identity $Group -Members $User.sAMAccountName -ErrorAction SilentlyContinue -Confirm:$false }
extsupportAsked:
Who is Participating?
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.

Mauro CazabonnetSenior Software EngineerCommented:
Try this
Import-module activedirectory
$Group = ".RBCO04_Management"
$Search = Get-ADOrganizationalUnit -Filter 'name -like "RBCO4"'
$pattern = '*RBCO MANAGER*'
ForEach ($OU in $Search)
{
	$OU.DistinguishedName
	$User = Get-ADUser -Filter {title -like $pattern} -properties Title -SearchBase "RBCO MANAGER" $OU.DistinguishedName
	$User.sAMAccountName
	$User | Foreach { Add-ADGroupMember -Identity $Group -Members $User.sAMAccountName -ErrorAction SilentlyContinue -Confirm:$false }
}

Open in new window

0
extsupportAuthor Commented:
Get-ADUser : A positional parameter cannot be found that accepts argument 'OU=RBCO4,OU=FL,OU=Field Centers,DC=Lincare,DC=com'.
At H:\Projects\InquisiqR4\Billing portal\add_inquisiq_Supervisor.ps1:8 char:10
+     $User = Get-ADUser -Filter {title -like $pattern} -properties Title -SearchBase ...
+    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
Add-ADGroupMember : Cannot validate argument on parameter 'Members'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At H:\Projects\InquisiqR4\Billing portal\add_inquisiq_Supervisor.ps1:10 char:64
+     $User | Foreach { Add-ADGroupMember -Identity $Group -Members $User.sAMAccountN ...
+                                                                   ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Add-ADGroupMember], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember
0
Mauro CazabonnetSenior Software EngineerCommented:
Updated Search base parameter

Import-module activedirectory
$Group = ".RBCO04_Management"
$Search = Get-ADOrganizationalUnit -Filter 'name -like "RBCO4"'
$pattern = '*RBCO MANAGER*'
ForEach ($OU in $Search)
{
	$OU.DistinguishedName
	$Users = Get-ADUser -SearchBase $OU.DistinguishedName -Filter { title -like $pattern } -properties SamAccountName, Title
	Foreach ($user in $Users)
	{
		$user.SamAccountName
		Add-ADGroupMember -Identity $Group -Members $User.sAMAccountName -ErrorAction SilentlyContinue -Confirm:$false
	}
}

Open in new window

0

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.