In PowerShell, we can use conditional statements, which allow scripts to make decisions based on certain conditions. As a PowerShell developer, you should know everything about it. In this tutorial, I will explain how to use the PowerShell if statement combined with the -or operator with some examples.
What is PowerShell If-Else with OR Condition?
In PowerShell, the if statement is used to evaluate a condition and execute a block of code if the condition is true. The -or operator is a logical operator that allows you to combine multiple conditions. If any of the combined conditions are true, the block of code within the if statement will execute. This is particularly useful when you need to check for multiple criteria.
Syntax of PowerShell if or
Here’s the basic syntax of an if statement with the -or operator in PowerShell:
if (condition1 -or condition2) {
# Code to execute if either condition1 or condition2 is true
} else {
# Code to execute if neither condition1 nor condition2 is true
}The if statement checks the conditions specified within the parentheses. If either condition1 or condition2 evaluates to true, the code inside the if block will run. If both conditions are false, the code inside the else block will execute.
Check out PowerShell If Else Statement to Check if a Number is Between Two Values
PowerShell If-Else with OR Condition Examples
Now, let me show you some real examples to understand how to work with PowerShell if else with OR conditions.
Example 1: Check User Access
Imagine you have a script that checks if a user has access to a system based on their role or department.
Then, you can write the below PowerShell script.
$role = "Administrator"
$department = "IT"
if ($role -eq "Administrator" -or $department -eq "IT") {
Write-Output "Access granted to the system."
} else {
Write-Output "Access denied."
}In this example, if the user is either an “Administrator” or belongs to the “IT” department, they will be granted access to the system.
I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

Check out How to Use Multiple Conditions in PowerShell If Else Statement?
Example 2: Validate User Input
Let’s say you want to validate user input to ensure it’s either “Yes” or “No”. Then, you can write the PowerShell script like the one below that uses the if or conditions.
$userInput = "Yes"
if ($userInput -eq "Yes" -or $userInput -eq "No") {
Write-Output "Valid input received."
} else {
Write-Output "Invalid input. Please enter 'Yes' or 'No'."
}In this script, if the user inputs either “Yes” or “No”, the script will acknowledge the valid input.
Here is the exact output in the screenshot below:

Example 3: Using Multiple Conditions
You can extend the if statement to include more than two conditions using the -or operator.
Let me show you an example.
$state = "California"
$city = "Los Angeles"
$zipcode = "90001"
if ($state -eq "California" -or $city -eq "Los Angeles" -or $zipcode -eq "90001") {
Write-Output "Location verified."
} else {
Write-Output "Location not verified."
}Here is the exact output in the screenshot below:

Check out PowerShell If Variable Contains
Example 4: Combine AND and OR Conditions
You can also combine -and and -or operators to create more complex conditions. Here is an example and the complete PowerShell script.
$age = 30
$citizenship = "USA"
$state = "Texas"
if (($age -ge 18 -and $citizenship -eq "USA") -or $state -eq "Texas") {
Write-Output "Eligible for the program."
} else {
Write-Output "Not eligible for the program."
}In this example, the user is eligible for the program if they are 18 or older and a USA citizen, or if they reside in Texas.
You can see in the screenshot below the exact output:

Conclusion
As a developer, you should understand how to work with PowerShell if statement with the -or operator. By combining multiple conditions, you can create powerful scripts that handle a variety of scenarios. I hope this tutorial has given you a clear understanding and practical examples of how to work with PowerShell if statement with or conditions.
You may also like:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.