# List multiple exclusions

**URL:** <https://forums.powershell.org/t/list-multiple-exclusions/12202>\
**Category:** PowerShell Help\
**Created:** [March 15, 2019, 1:33pm UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202 "2019-03-15T13:33:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![br456](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@br456](https://forums.powershell.org/u/br456)\
**Post date:** [March 15, 2019, 1:33pm UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/1 "2019-03-15T13:33:20Z")

</div>

Hi all,

It’s been a while since my last post, but, I have a business need to create a clone of an AD user members security groups to a couple of new hires. I have the code written that I need to do this, however, there are multiple AD security groups that can NOT be added and I need them excluded in the script. I am using where {$\_ -notlike as my filtering system but from trial and error, it appears that only works to filter out ONE AD security group and not multiple.

Does anyone have any suggestions? I am still relatively new to powershell. I’ll need help as soon possible, thanks!  
Code is below (names modified for confidentiality):

This is the code that works but does NOT have multiple exclusions:

```
Get-ADUser -Identity xyuser -Properties memberof |
Select-Object -ExpandProperty memberof |
Where{$_ -notlike '*TEST1*'}|
Add-ADGroupMember -Members xyuser -Confirm -Verbose
```

This is the code that has the exclusions, but does not work. The output gives the intended AD user all the security groups:

```
Get-ADUser -Identity xyuser -Properties memberof |
Select-Object -ExpandProperty memberof |
Where{$_ -notlike '*TEST1*','*TEST2*','*TEST3*'}|
Add-ADGroupMember -Members xyuser -Confirm -Verbose
```

---

<div class="post-metadata">

**Author:** ![br456](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@br456](https://forums.powershell.org/u/br456)\
**Post date:** [March 15, 2019, 1:59pm UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/2 "2019-03-15T13:59:57Z")

</div>

I actually think I may have figured it out:

```
Get-ADUser -Identity xyuser -Properties memberof |
Select-Object -ExpandProperty memberof |
Where{$_ -notlike '*TEST1*'}|
Where{$_ -notlike '*TEST2*'}|
Where{$_ -notlike '*TEST3*'}|
Add-ADGroupMember -Members xyuser -Confirm -Verbose
```

This worked for me when I ran it on my 2012R2 DEV Lab server…  
Any suggestions for better code are welcome, however!

---

<div class="post-metadata">

**Author:** ![firmbyte](https://avatars.discourse-cdn.com/v4/letter/f/e68b1a/32.png) [@firmbyte](https://forums.powershell.org/u/firmbyte)\
**Post date:** [March 15, 2019, 2:01pm UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/3 "2019-03-15T14:01:13Z")

</div>

How about using -NotMatch ?

```
{$\_ -notMatch "TEST1|TEST2|TEST3"}
```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [March 15, 2019, 2:09pm UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/4 "2019-03-15T14:09:32Z")

</div>

match works, But it will exclude if a Group name contains that word, you can try -notcontains and -notin as well.

```
$Exclude = 'a','b','c'
'c' -notin $Exclude

#Or

$Exclude -notcontains 'c'
```

---

<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:** [March 15, 2019, 3:43pm UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/5 "2019-03-15T15:43:30Z")

</div>

You can also chain multiple exlusions with an operator like **-and** or **-or** 😉

```
Get-ADUser -Identity xyuser -Properties memberof |
Where-Object {$_.memberof -notlike ‘TEST1’ -and  
$_.memberof -notlike ‘_TEST2_’ -and
$_.memberof -notlike ‘_TEST3_’} |
Add-ADGroupMember -Members xyuser -Confirm -Verbose
```

---

<div class="post-metadata">

**Author:** ![br456](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@br456](https://forums.powershell.org/u/br456)\
**Post date:** [March 18, 2019, 7:08am UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/6 "2019-03-18T07:08:58Z")

</div>

[quote quote=144950][/quote]

Olaf–I tried your suggestion and it doesn’t seem to work as intended. After running it in my environment, all it does it copy the AD security groups completely, instead of making any exclusions in the suggested operators.

---

<div class="post-metadata">

**Author:** ![br456](https://avatars.discourse-cdn.com/v4/letter/b/a587f6/32.png) [@br456](https://forums.powershell.org/u/br456)\
**Post date:** [March 18, 2019, 7:39am UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/7 "2019-03-18T07:39:13Z")

</div>

[quote quote=144933]How about using -NotMatch ?

PowerShell

3 lines

\<textarea class="ace\_text-input" wrap="off" autocorrect="off" autocapitalize="off" spellcheck="false" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;"\>\</textarea\>

1

2

3

{$\_ -notMatch "TEST1|TEST2|TEST3"}

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

[/quote]

Iain, I tried this as well and it resulted in the same as the above: all groups went through the pipeline with the exclusions.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [March 18, 2019, 8:21am UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/8 "2019-03-18T08:21:54Z")

</div>

What exactly are you running? These seem to work:

```
get-aduser user1 -Properties memberof | 
  select-object -expandproperty memberof | 
  where-object {$_ -notmatch 'group1|group2'} | 
  Add-ADGroupMember -members user2 -whatif

get-aduser user1 -Properties memberof | 
  select-object -expandproperty memberof | 
  where-object {$_ -notlike '*group1*' -and $_ -notlike '*group2*'} | 
  Add-ADGroupMember -members user2 -whatif

get-aduser user1 -Properties memberof | 
  select-object -expandproperty memberof | 
  where-object {$_ -notin 'group1fullname','group2fullname'} | 
  Add-ADGroupMember -members user2 -whatif
```

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [March 18, 2019, 10:01am UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/9 "2019-03-18T10:01:28Z")

</div>

@br456.have you tried -notin / -notcontinas ?

---

<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:33pm UTC](https://forums.powershell.org/t/list-multiple-exclusions/12202/10 "2024-05-16T20:33:09Z")

</div>

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


