Comparisons on Multiple Conditions

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.



Try using the -like operator instead of -eq.

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.


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!"

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) {

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.

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.

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.

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’) }