# Array comparision doesn't work with contanins and notcontains

**URL:** <https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557>\
**Category:** PowerShell Help\
**Created:** [October 26, 2018, 5:20am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557 "2018-10-26T05:20:21Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![iarunesh](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@iarunesh](https://forums.powershell.org/u/iarunesh)\
**Post date:** [October 26, 2018, 5:20am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/1 "2018-10-26T05:20:21Z")

</div>

```
$argu1 = Get-ChildItem C:\ProgramData\Citrix\GroupPolicy -Exclude Rollback.gpf,Rsop.gpf | Split-Path -Leaf

$argu2 = Get-ChildItem HKLM:\SOFTWARE\Policies\Citrix | Split-Path -Leaf
$common = $argu1 | ? {if($argu2 -contains $_){$_}} # find the common.
Add-PSSnapin Citrix* -ErrorAction SilentlyContinue
if($Ctxsession=@(Get-XASession -LocalhostOnly | Where-Object { $_.State -match 'Active' -and $_.Protocol -match 'ICA'} | Select-Object -ExpandProperty SessionID | Format-Table -HideTableHeaders))
{
$final = Compare-Object $Ctxsession $common | Where-Object {$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject | Format-Table -HideTableHeaders **$exclude** = $final | where {$Ctxsession -notcontains $_} # $exclude gives all values and doesn't work as expected and if I use contains operator $exclude is empty

}

else{Write-Host "Didn't find anything"}
```

---

<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:** [October 26, 2018, 6:06am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/2 "2018-10-26T06:06:37Z")

</div>

You didn’t mention the problem you are facing. Please tell us what error/unexpected behavior you get using the above code.  
And please edit the post and format the code properly. Let us know if you find it difficult to format the code.

---

<div class="post-metadata">

**Author:** ![iarunesh](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@iarunesh](https://forums.powershell.org/u/iarunesh)\
**Post date:** [October 26, 2018, 6:18am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/3 "2018-10-26T06:18:52Z")

</div>

$ctxsession and $final or other variables mentioned in the code are array objects.

**$Ctxsession** has random values which are in the **$final** as well. while $final holds more array structured numerical values. **example :**

PS C:\Windows\system32\> $Ctxsession  
10  
11

PS C:\Windows\system32\> $final  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
2  
20

I am trying to get the values which are not common ; where the operators being used as -notcontains but **$exclude** give all values which is being held by $final.

hope would be clear now.

&nbsp;

&nbsp;

---

<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:** [October 26, 2018, 6:24am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/4 "2018-10-26T06:24:07Z")

</div>

How about something like this:

```
$Ctxsession = @(10,11)
$final = @(10,11,12,13,14,15,16,17,18,19,2,20)
Compare-Object -ReferenceObject $Ctxsession -DifferenceObject $final
```

---

<div class="post-metadata">

**Author:** ![iarunesh](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@iarunesh](https://forums.powershell.org/u/iarunesh)\
**Post date:** [October 26, 2018, 6:39am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/5 "2018-10-26T06:39:40Z")

</div>

$ctxsession and $final holds the runtime value not the static.

---

<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:** [October 26, 2018, 7:06am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/6 "2018-10-26T07:06:47Z")

</div>

That’s just an example!! Of course you can use whatever data you like and do a comparison. 😉

---

<div class="post-metadata">

**Author:** ![iarunesh](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@iarunesh](https://forums.powershell.org/u/iarunesh)\
**Post date:** [October 26, 2018, 7:21am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/7 "2018-10-26T07:21:14Z")

</div>

Sorry, But it didn’t provide the expected result. is there any issue with code line 10.

---

<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:** [October 26, 2018, 7:28am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/8 "2018-10-26T07:28:11Z")

</div>

Compare-Object should work. Did you try -notin operator ?

```
$final | Where-Object -FilterScript {$_ -notin $Ctxsession}
```

---

<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:** [October 26, 2018, 8:24am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/9 "2018-10-26T08:24:11Z")

</div>

> [@](#):
>
> Sorry, But it didn’t provide the expected result. is there any issue with code line 10.

My code example had 3 lines!  
You wrote before:

> [@](#):
>
> I am trying to get the values which are not common

… and that’s what my code example does. Did you try? Did you try to understand the code and the concept behind? 😉

---

<div class="post-metadata">

**Author:** ![iarunesh](https://avatars.discourse-cdn.com/v4/letter/i/a5b964/32.png) [@iarunesh](https://forums.powershell.org/u/iarunesh)\
**Post date:** [October 26, 2018, 8:29am UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/10 "2018-10-26T08:29:09Z")

</div>

Found the problem with $exclude ; as it should have been declared as empty array initially; which wasn’t done there.

$exclude = @()

$exclude = $final | where {$Ctxsession -notcontains $\_}

&nbsp;

---

<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:34pm UTC](https://forums.powershell.org/t/array-comparision-doesnt-work-with-contanins-and-notcontains/11557/11 "2024-05-16T20:34:47Z")

</div>


