How to logical OR multiple variables in Powershell

I thought this was simple. And I’m sure it is. But, I can’t seem to crack it.

I have 3 functions that return a true or false value.

In a later if evaluation I am trying to logical or the 3 results together.

Something like this:

if (Fnc1 -or Fnc2 -or Fnc3) { write-host "Yes" }

Not only is Powershell highlighting the syntax differently for the first Fnc1 from the others, it’s only returning true or false based on the value of Fnc1 from what I can tell.

I know this works:

if ((Fnc1 -eq $true) -or (Fnc2 -eq $true) -or (Fnc3 -eq $true)) { write-host "Yes" }

But, that seems like overkill and un-necessary.

What am I missing?

>Solution :

PowerShell attempts to parse the -or token as a function parameter when you place it after a function name like that. Surround each function call in ():

if ((Fnc1) -or (Fnc2) -or (Fnc3)) { write-host "Yes" }

Leave a Reply