# Newbie needs help - Shadow Groups

**URL:** <https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006>\
**Category:** PowerShell Help\
**Created:** [February 7, 2017, 1:06pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006 "2017-02-07T13:06:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjascob9](https://avatars.discourse-cdn.com/v4/letter/b/f17d59/32.png) [@bjascob9](https://forums.powershell.org/u/bjascob9)\
**Post date:** [February 7, 2017, 1:06pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006/1 "2017-02-07T13:06:18Z")

</div>

Hi, i’m new to using Powershell and am trying to create a script to automatically add all users with a specific attribute in AD to a Group and remove any from that group that do not have this attribute. I have just one section of a line that is not working and keeps giving errors. Any assistance would be great

Here is what i have:

#Import the AD module  
ipmort-module ActiveDirectory

#Set your search OU and Group Variables  
$OU=“OU=_ **,DC=** _,DC=com”  
$Group=“CN=GroupNameHere,OU=_ **,DC=** _,DC=com”

#Pull current list of Group Members and remove anyone not a Regular Employee - This part works fine  
Get-ADGroupMember –Identity $Group | Where-Object {$_.extensionAttribute2 –NotMatch “Regular”} | ForEach-Object {Remove-ADPrincipalGroupMembership –Identity $_ –MemberOf $Group –Confirm:$false}

#Adds any Regular employee to the Group that currently is not a member of it - This is where my issue is. The LDAPfilter to check if user already in a group is not working. I want to find any user with the attribute listed set to 'Regular" and add them to this group if they are not already in it.

Get-ADUser -Filter {extensionAttribute2 -like “Regular”} –SearchBase $OU –SearchScope Subtree –LDAPFilter “(!memberOf=$Group)” | ForEach-Object {Add-ADPrincipalGroupMembership –Identity $\_ –MemberOf $Group}

---

<div class="post-metadata">

**Author:** ![techtim2003yahoo-com](https://avatars.discourse-cdn.com/v4/letter/t/dfb087/32.png) [@techtim2003yahoo-com](https://forums.powershell.org/u/techtim2003yahoo-com)\
**Post date:** [February 7, 2017, 1:12pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006/2 "2017-02-07T13:12:50Z")

</div>

What happens when you break it down?

```
Get-ADUser -Filter {extensionAttribute2 -like "Regular"} –SearchBase $OU –SearchScope Subtree –LDAPFilter “(!memberOf=$Group)”
```

I would expect this to return all results. Worry about the later action after you get this core piece working. If I remember correctly (I am a bit of a newb myself) -like requires wildcards. so “_regular_”

---

<div class="post-metadata">

**Author:** ![bjascob9](https://avatars.discourse-cdn.com/v4/letter/b/f17d59/32.png) [@bjascob9](https://forums.powershell.org/u/bjascob9)\
**Post date:** [February 7, 2017, 1:18pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006/3 "2017-02-07T13:18:04Z")

</div>

This portion works fine running by itself: Get-ADUser -Filter {extensionAttribute2 -like “Regular”} –SearchBase $OU –SearchScope Subtree

once i add –LDAPFilter “(!memberOf=$Group)” it gives the below error:

Get-ADUser : Parameter set cannot be resolved using the specified named parameters.  
At line:1 char:1

- Get-ADUser -Filter {extensionAttribute2 -like “Regular”} –SearchBase …
- 

```auto
  + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ParameterBindingException
  + FullyQualifiedErrorId : AmbiguousParameterSet,Microsoft.ActiveDirectory.Management.Commands.GetADUser
```

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [February 7, 2017, 1:37pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006/4 "2017-02-07T13:37:48Z")

</div>

Please post the error message. From a quick glance your last statement is not correct. You can’t combine -Filter and -LDAPFilter in the same command. Either settle on -Filter or -LDAPFilter.

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [February 7, 2017, 1:45pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006/5 "2017-02-07T13:45:01Z")

</div>

Example:

```
Get-ADUser -LDAPFilter "(&(extensionAttribute2=Regular*)(!memberOf=$Group))" –SearchBase $OU –SearchScope Subtree | ForEach-Object { Add-ADPrincipalGroupMembership –Identity $_ –MemberOf $Group }
```

---

<div class="post-metadata">

**Author:** ![bjascob9](https://avatars.discourse-cdn.com/v4/letter/b/f17d59/32.png) [@bjascob9](https://forums.powershell.org/u/bjascob9)\
**Post date:** [February 7, 2017, 3:28pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006/6 "2017-02-07T15:28:02Z")

</div>

Thanks…That solved my issue. Really appreciate the help.

---

<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:39pm UTC](https://forums.powershell.org/t/newbie-needs-help-shadow-groups/8006/7 "2024-05-16T20:39:31Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


