# remove Users from All ad groups except 2

**URL:** <https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722>\
**Category:** PowerShell Help\
**Created:** [January 28, 2020, 1:08am UTC](https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722 "2020-01-28T01:08:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [January 28, 2020, 1:08am UTC](https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722/1 "2020-01-28T01:08:02Z")

</div>

Hi  
I have found this script which works perfect for to remove the user from all groups but 1.  
However I need to remove the user from All groups but 2 what do I need to adjust to keep just 2 groups active  
Domain users and syncedToAzure needs to remain  
[pre]

$users = import-csv c:\temp\toRemove.csv

foreach ($user in $users)  
{  
$adgroups = Get-ADPrincipalGroupMembership -Identity $user.SamAccountName  
foreach ($singlegroup in $adgroups)  
{  
if ($singlegroup.SamAccountName -notlike “_Domain Users_”)  
{  
Remove-ADPrincipalGroupMembership -Identity $user.SamAccountName -MemberOf $singlegroup.SamAccountName -confirm:$false  
}  
}  
}  
[/pre]  
thanks for your assistance

---

<div class="post-metadata">

**Author:** ![jcstdio](https://avatars.discourse-cdn.com/v4/letter/j/a9adbd/32.png) [@jcstdio](https://forums.powershell.org/u/jcstdio)\
**Post date:** [January 28, 2020, 5:27am UTC](https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722/2 "2020-01-28T05:27:21Z")

</div>

Hi,

As I recall, you can do it with the -Or option, changing that line:

```
if ($singlegroup.SamAccountName -notlike "*Domain Users*" -Or $singlegroup.SamAccountName -notlike "syncedToAzure")
```

Hope it helps

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [January 28, 2020, 7:05am UTC](https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722/3 "2020-01-28T07:05:41Z")

</div>

when I’m trying this I get the following error

WARNING: Could not remove member(s) from ADGroup: ‘CN=Domain Users,CN=Users,DC=synamedia,DC=com’. Error is: ‘The user cannot be removed from a group because the group is currently th  
e user’s primary group’.  
Remove-ADPrincipalGroupMembership : Could not remove member(s) to one or more ADGroup.  
At C:\Users\username\Documents\removeUsersFromGroup.ps1:14 char:5

- Remove-ADPrincipalGroupMembership -Identity $user.SamAccoun …
- 

```auto

```

- CategoryInfo : OperationStopped: (Microsoft.Activ…ement.ADGroup:ADGroup) [Remove-ADPrincipalGroupMembership], ADException
- FullyQualifiedErrorId : 1,Microsoft.ActiveDirectory.Management.Commands.RemoveADPrincipalGroupMembership

---

<div class="post-metadata">

**Author:** ![wightsci](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/wightsci/32/73_2.png) [@wightsci](https://forums.powershell.org/u/wightsci)\
**Post date:** [January 28, 2020, 12:41pm UTC](https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722/4 "2020-01-28T12:41:21Z")

</div>

Try changing the **-Or** to **-And**. That should do the trick. You need the group name to not be ‘Domain Users’ **and also** not be ‘synchedToAzure’.

Regards,

Stuart.

---

<div class="post-metadata">

**Author:** ![acer4605](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@acer4605](https://forums.powershell.org/u/acer4605)\
**Post date:** [January 28, 2020, 12:50pm UTC](https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722/5 "2020-01-28T12:50:43Z")

</div>

thanks Stuard for the tip that solved my issue  
Best regards

Paul

---

<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:30pm UTC](https://forums.powershell.org/t/remove-users-from-all-ad-groups-except-2/13722/6 "2024-05-16T20:30:46Z")

</div>

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


