Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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" }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading