Add multiple security groups to computers using PowerShell

Help with script that can add multiple member groups to computers.

My script works if I only get one member group and assign it to a variable but when I assign multiple groups to a variable it doesn't like it. Someone please feel free to school me here.

# Computer Path to my computers in ADUC
$CP = "OU=folder,DC=domain,DC=com"

# Group Path to my member groups
$GP = "OU=Groups,DC=domain,DC=com"

# Groups that I want to add to computers
$SecGroup = Get-ADGroup -Filter {Name -like 'MemGroup1' -or Name -like 'MemGroup2' -or Name -like 'MemGroup3'} -SearchBase $GP

$C = Get-ADComputer -Filter 'Name -like "ComputerName"' -Properties * -SearchBase $CP

PS > Add-ADGroupMember $SecGroup -Members $C

Add-ADGroupMember : Cannot convert 'System.Object[]' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported.

Open in new window

This works however if I change my $SecGroup variable to only grab one member group. But if I add more than one group then it breaks?

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.

In the following Syntax.. You can see the the Identity parameter doesn't accept an array of values like Members parameter..
Add-ADGroupMember -Identity <ADGroup> -Members <ADPrincipal[]>

Open in new window

You can use Foreach to loop the groups to assign membership..
$SecGroup | %{Add-ADGroupMember $_.Name -Members $C}

Open in new window


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
ryanmavesAuthor Commented:

I was trying to use the ForEach-Object cmdlet but looks like I was using it incorrectly. Thanks for the tip it even works if I assign multiple computers to $C, excellent!
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

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.