# Can't add computer to ad group

**URL:** <https://forums.powershell.org/t/cant-add-computer-to-ad-group/12736>\
**Category:** PowerShell Help\
**Created:** [June 18, 2019, 9:03am UTC](https://forums.powershell.org/t/cant-add-computer-to-ad-group/12736 "2019-06-18T09:03:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tony-rodal](https://avatars.discourse-cdn.com/v4/letter/t/48db29/32.png) [@tony-rodal](https://forums.powershell.org/u/tony-rodal)\
**Post date:** [June 18, 2019, 9:03am UTC](https://forums.powershell.org/t/cant-add-computer-to-ad-group/12736/1 "2019-06-18T09:03:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 18, 2019, 11:00am UTC](https://forums.powershell.org/t/cant-add-computer-to-ad-group/12736/2 "2019-06-18T11:00:40Z")

</div>

> [@](#):
>
> … but I’m having trouble with this certain group.

What kind of trouble?

> [@](#):
>
> … I believe the problem is the parentheses but I’m not sure.

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’
```

---

<div class="post-metadata">

**Author:** ![tony-rodal](https://avatars.discourse-cdn.com/v4/letter/t/48db29/32.png) [@tony-rodal](https://forums.powershell.org/u/tony-rodal)\
**Post date:** [June 18, 2019, 11:56am UTC](https://forums.powershell.org/t/cant-add-computer-to-ad-group/12736/3 "2019-06-18T11:56:24Z")

</div>

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’

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 18, 2019, 4:24pm UTC](https://forums.powershell.org/t/cant-add-computer-to-ad-group/12736/4 "2019-06-18T16:24:41Z")

</div>

Without you showing the error you get if you get one it’s hard to say what problem you have. 😉 If you use the [-match operator](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_comparison_operators?view=powershell-5.1)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](https://en.wikipedia.org/wiki/Regular_expression).

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/cant-add-computer-to-ad-group/12736/5 "2024-05-16T20:32:06Z")

</div>


