# Comparisons on Multiple Conditions

**URL:** <https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090>\
**Category:** PowerShell Help\
**Created:** [October 23, 2015, 1:55am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090 "2015-10-23T01:55:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![cody-everingham](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@cody-everingham](https://forums.powershell.org/u/cody-everingham)\
**Post date:** [October 23, 2015, 1:55am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/1 "2015-10-23T01:55:42Z")

</div>

I am trying to write a script to check multiple conditions at once. Basically I want to do the following.

$roo = 3  
if ($roo -eq [1-5]) {  
write-output “WORKS!”  
}

however "-eq [1-5]) " is the issue. I get the following error:  
“You must provide a value expression on the right-hand side of the ‘-eq’ operator.”

how do I get $roo to check against 1, 2, 3, 4, and 5. And I do know I could use -or to do this but my real situation includes much more than 5 conditions.

PLEASE HELP :D!

Thanks

---

<div class="post-metadata">

**Author:** ![christopher-wolfende](https://avatars.discourse-cdn.com/v4/letter/c/e95f7d/32.png) [@christopher-wolfende](https://forums.powershell.org/u/christopher-wolfende)\
**Post date:** [October 23, 2015, 1:58am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/2 "2015-10-23T01:58:15Z")

</div>

Try using the -like operator instead of -eq.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 23, 2015, 2:01am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/3 "2015-10-23T02:01:51Z")

</div>

Well, I’m assuming you mean “against 1, 2, 3, 4, OR 5.” It would be impossible for something to be equal to both 1 and 2.

It depends a bit on what $roo is supposed to contain. If $roo will contain an integer, then:

if ($roo -in @(1,2,3,4,5)) {}

Would check to see if $roo contained one of those integers. If $roo contains a string consisting only of “1”,“2”, etc. then:

if ($roo -in @(‘1’,‘2’,‘3’,‘4’,‘5’)) {}

However, is $roo contained something like “5.0” or “1.1” then there’s no easy way to provide a match set like you’re trying to do. PowerShell doesn’t implicitly match against array elements like some other languages (and, in any event, your original error was that you were using square brackets incorrectly). You’d could do:

if ($roo -in @(‘1.0’,‘2.2’,‘3.1’,‘4.0’,‘5.7’)) {}

To match from a set of specific strings.

---

<div class="post-metadata">

**Author:** ![tim-curwick](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tim-curwick/32/431_2.png) [@tim-curwick](https://forums.powershell.org/u/tim-curwick)\
**Post date:** [October 23, 2015, 2:06am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/4 "2015-10-23T02:06:33Z")

</div>

Cody,

You may be looking for this. This will match any integer between 1 and 5.

```
$roo = 3
if ($roo -in 1..5) {
write-output "WORKS!"
}
```

---

<div class="post-metadata">

**Author:** ![cody-everingham](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@cody-everingham](https://forums.powershell.org/u/cody-everingham)\
**Post date:** [October 23, 2015, 2:21am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/5 "2015-10-23T02:21:05Z")

</div>

Thank you so much for the reply. I’m still having little luck, so I will provide my exact issue and see if anyone understands what I’m trying too ask.

$roomNumber = $env:computername.subString(0,$env:computername.length-2)  
if ($roomnumber -eq ‘129M’ -or $roomnumber -eq ‘130M’ -or $roomnumber -eq ‘131M’ -or $roomnumber -eq ‘132M’) {  
(New-Object -COM WScript.Network).AddWindowsPrinterConnection(‘\Printerserver\132M\_C01’)  
}

The above program works great in my environment but I’m going to be adding many more computers and its not ideal to continue adding -or(s). So I tried this.

$roomnumber = $env:computername.subString(0,$env:computername.length-2)  
if ($roomnumber -eq /^1[2-3][0-4]M) {  
WScript.Network).AddWindowsPrinterConnection(‘\Printerserver\132M\_C01’)  
}

Also just for reference a $roomnumber will look something like “132M”.

Saddly this didnt work for me earlier.  
$roo = 3  
if ($roo -in @(‘1’,‘2’,‘3’,‘4’,‘5’)) {  
write-output “WORKS!”  
}  
I got a lot of missing statement blocks after the condition.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [October 23, 2015, 2:34am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/6 "2015-10-23T02:34:03Z")

</div>

The -eq operator means EXACTLY EQUAL TO. The way you’re using it is correct in some instances, but incorrect in others - it doesn’t process regular expressions, for example. You’d need to use -match in that case, for your $roomnumber comparison.

Additionally, PowerShell typically would put the regex into quotation marks, rather than using slashes as the delimiter, although .NET makes some allowances in that area.

What might make it easier to help you is if you just pull out one comparison at a time that you’re having a problem with, and let us hash through that one. Looking at your script, aside from your use of -eq rather than -match, I’m not sure where you’re actually having a problem.

---

<div class="post-metadata">

**Author:** ![cody-everingham](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@cody-everingham](https://forums.powershell.org/u/cody-everingham)\
**Post date:** [October 23, 2015, 4:27am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/7 "2015-10-23T04:27:45Z")

</div>

THANKS!!! You guys got me on the right path. This is what fixed it.

if ($roomnumber -eq ‘129M’ -or $roomnumber -match ‘^13+[0-8]+M$’) {

this effectively checks to see if $roomnumber is 129M or 130M, 131M, 132M, 133M, 134M, 135M, 136M, 137M, 138M.

Again, Thank you.

---

<div class="post-metadata">

**Author:** ![mark-stone](https://avatars.discourse-cdn.com/v4/letter/m/898d66/32.png) [@mark-stone](https://forums.powershell.org/u/mark-stone)\
**Post date:** [October 23, 2015, 7:23am UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/8 "2015-10-23T07:23:24Z")

</div>

If you intend to use this method for setting multiple printers for multiple locations, this might scale better. Especially if your room sets are non-sequential.

$Printer132MRooms = @(“129M”,“130M”,“131M”,“132M”,“133M”,“134M”,“135M”,“136M”,“137M”,“138M”)  
$PrinterxxxMRooms = @(“xxxM”,“xxxM”,“xxxM”,“xxxM”,“xxxM”,“xxxM”,“xxxM”,“xxxM”,“xxxM”,“xxxM”)

$roomNumber = $env:computername.subString(0,$env:computername.length-2)  
If ($roomnumber -in $Printer132MRooms) { (New-Object -COM WScript.Network).AddWindowsPrinterConnection(‘\Printerserver\132M\_C01’) }  
If ($roomnumber -in $PrinterxxxMRooms) { (New-Object -COM WScript.Network).AddWindowsPrinterConnection(‘\Printerserver\xxxM\_C01’) }

---

<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:44pm UTC](https://forums.powershell.org/t/comparisons-on-multiple-conditions/5090/9 "2024-05-16T20:44:34Z")

</div>

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


