Can't add computer to ad group

Hi, I have been using the following script to add a computer to AD groups but I’m having trouble with this certain group. I believe the problem is the parentheses but I’m not sure.

$computername = ‘705pqg2’

$dns = get-adcomputer $computername

$b = $dns.distinguishedname

Add-ADPrincipalGroupMembership $b (Get-ADGroup -Filter * -Server ‘test’ | where { $_.name -match ‘Apps - Java JRE (test)_Dom1’ })

Any help would be greatly appreciated.

Thanks

What kind of trouble?

Why do you think that?
Don’t you know the exact name of the group? … or why do you use a where filter with a match comparison?

If it’s a single computer and a single group you could use

Add-ADGroupMember -Identity ‘Apps – Java JRE (test)_Dom1’ -Members ‘705pqg2’

Hi yes that would work and thank you. That is the exact name. I typically add multiple groups using a similar script. I guess I was curious on why no groups with the parentheses worked. This works also:

$computername = ‘705pqg2’

$dns = get-adcomputer $computername

$b = $dns.distinguishedname

Add-ADPrincipalGroupMembership $b ‘Apps – Java JRE (test)_Dom1’

Without you showing the error you get if you get one it’s hard to say what problem you have. :wink: If you use the -match operator you should be aware of that it’s using regular expressions for comparison. Therefor charachters might not have the meaning you expect. You might read some more information about that topic - regular expressions.

BTW: When you post code please format it as code using the code tag button (pre) on the edit bar of the post editor. Thanks.