# If A or B equals C

**URL:** <https://forums.powershell.org/t/if-a-or-b-equals-c/13020>\
**Category:** PowerShell Help\
**Created:** [August 20, 2019, 10:32am UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020 "2019-08-20T10:32:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rich123](https://avatars.discourse-cdn.com/v4/letter/r/7ea924/32.png) [@rich123](https://forums.powershell.org/u/rich123)\
**Post date:** [August 20, 2019, 10:32am UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/1 "2019-08-20T10:32:00Z")

</div>

Hi All,

I was writing a function today where I need the outcome to be true if A or B equal C, where see is the results of a Get-ItemProperty

Originally I had:

If $a -eq Get-ItemProperty…

Of course in order to avoid running the Get-ItemProperty cmdlet twice (due to needing to comapre it against $b as well) I have assigned the results to variable $C for use in the if statement.

Normally I would write this If A equals C or If B Equals C but was wondering, is there is a way to structure this as If A or B equal C?

Not really a big deal I was just curious if it were possible.

Thanks

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [August 20, 2019, 10:38am UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/2 "2019-08-20T10:38:20Z")

</div>

Yep, more or less. 🙂

```
$a = 1
$b = 3

$value = 1
if ($value -in $a, $b) {
    "it's either $a or $b, not sure which"
}
```

---

<div class="post-metadata">

**Author:** ![rich123](https://avatars.discourse-cdn.com/v4/letter/r/7ea924/32.png) [@rich123](https://forums.powershell.org/u/rich123)\
**Post date:** [August 20, 2019, 11:06am UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/3 "2019-08-20T11:06:02Z")

</div>

Thanks Joel, appreciate you taking the time to reply, I will make a note of the example as it may come in handy in the future.

Thanks again.

---

<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:** [August 20, 2019, 2:35pm UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/4 "2019-08-20T14:35:15Z")

</div>

You could use the fact that -eq works differently with arrays, and returns the item if it’s in the list.

```
$a,$b,$c=1,2,2

$a,$b -eq $c
2

if ($a,$b -eq $c) { 'yes' }
yes

$a,$b,$c=1,2,3

$a,$b -eq $c
# null

if ($a,$b -eq $c) { 'yes' }
```

There is a kind of “or” regex too.

```
$c -match "$a|$b"
```

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [August 21, 2019, 7:11am UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/5 "2019-08-21T07:11:08Z")

</div>

Yep, definitely possible. I’d probably not want to do the first one there if the array is fairly large, it’d make PS do a lot of extra work (building a whole new array, etc.) that isn’t needed in this instance.

Good call on `-match`, but important to remember it only works if your values are strings, or can be accurately represented when converted to strings.

---

<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:** [August 21, 2019, 9:41am UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/6 "2019-08-21T09:41:41Z")

</div>

Hmm, somehow with foreach there’s no extra memory being used (the WS doesn’t go up). And I used to correct people on this.

```
foreach ($i in 1..10000000) { $i;sleep 1}

# another window
ps powershell

Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
    554 29 78136 88100 1.27 2848 59 powershell
    577 29 62704 73232 3.63 11460 59 powershell
```

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [August 21, 2019, 11:43am UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/7 "2019-08-21T11:43:22Z")

</div>

Yeah, foreach just accesses each item in the array in turn, so it won’t use much, if any, extra memory. However, creating a new array does create some overhead in terms of extra memory (the amount will of course still vary based on the sizes of the arrays involved and what they contain).

---

<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:32pm UTC](https://forums.powershell.org/t/if-a-or-b-equals-c/13020/8 "2024-05-16T20:32:41Z")

</div>

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


