# \-contains vs .contains()

**URL:** <https://forums.powershell.org/t/contains-vs-contains/3472>\
**Category:** PowerShell Help\
**Created:** [December 2, 2014, 8:35am UTC](https://forums.powershell.org/t/contains-vs-contains/3472 "2014-12-02T08:35:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephenmbell](https://avatars.discourse-cdn.com/v4/letter/s/3be4f8/32.png) [@stephenmbell](https://forums.powershell.org/u/stephenmbell)\
**Post date:** [December 2, 2014, 8:35am UTC](https://forums.powershell.org/t/contains-vs-contains/3472/1 "2014-12-02T08:35:56Z")

</div>

This may be a easy question, but I am a bit confused. I have a script that I am using in a point-of-sale environment. 700 PCs. Each named consistently, like this - POS\_A\_[NUMBER] for each location. For example, POS\_A\_101, POS\_B\_101, POS\_A\_102, POS\_B\_102, etc…all the way to 700.

In my script, I am running a different process depending on if the machine is an A machine or a B machine.

I do the following:

```
if($env:COMPUTERNAME.ToLower() -contains "_A_")
{
# do some stuff
}
```

Problem is, if I run:

```
$env:COMPUTERNAME.ToLower() -contains "_a_"
```

On a machine that is named POS\_A\_205, I get a return of False.

However, if I run:

```
$env:COMPUTERNAME.ToLower().Contains("_a_")
```

On a machine that is named POS\_A\_205, I get a return of True

What’s the difference? Obviously, I know how to modify the script to function like I want, I just don’t get why one is different than the other?

Thanks in advance,  
sb

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [December 2, 2014, 9:17am UTC](https://forums.powershell.org/t/contains-vs-contains/3472/2 "2014-12-02T09:17:54Z")

</div>

I’d start by reading the ‘-Contains isn’t -Like’ chapter in The Big Book of PowerShell Gotchas ([https://onedrive.live.com/?cid=7F868AA697B937FE&amp;id=7F868AA697B937FE!112](https://onedrive.live.com/?cid=7F868AA697B937FE&amp;id=7F868AA697B937FE!112)). Then I’d move away from using contains to using -like, such as If ($env:COMPUTERNAME.ToLower() -like ‘_A_’) {}

---

<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:** [December 2, 2014, 9:20am UTC](https://forums.powershell.org/t/contains-vs-contains/3472/3 "2014-12-02T09:20:01Z")

</div>

Hi Stephen,

The PowerShell Containment operators (-Contains and -NotCOntains) are designed to work against collections like arrays not strings like $env:COMPUTERNAME. The containment operators only match exact values.

Examples from about\_Comparison\_Operators help:

```
PS C:\> "abc", "def" -Contains "def"
True

PS C:\> "Windows", "PowerShell" -Contains "Shell"
False #Not an exact match
```

The correct PowerShell operators to use in your case are -match or -like which are case-insensitive. No need to convert to lower case.

Example:

```
$env:COMPUTERNAME
POS_A_205

$env:COMPUTERNAME -match '_a_'
True

$env:COMPUTERNAME -like '*_a_*'
True
```

I hope that helps.

Regards,  
Daniel

---

<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:46pm UTC](https://forums.powershell.org/t/contains-vs-contains/3472/4 "2024-05-16T20:46:42Z")

</div>


