PowerShell If Else Statement to Check if a Number is Between Two Values

Recently, I was working on a PowerShell script that wanted to check if a number is between two values in an if-else statement. I tried a few examples. In this tutorial, I will show you how to check if a number is between two values in a PowerShell if-else statement.

Check if a Number is Between Two Values in a PowerShell If Else Statement

If you are new to PowerShell, here is the syntax of a PowerShell if statement.

if (condition) {
    # Code to execute if the condition is true
}

The condition is an expression that evaluates to either $true or $false. If the condition is true, the code block inside the curly braces {} is executed. Otherwise, it is skipped.

Now, let us check out how to check a number between two values in a PowerShell if-else statement.

To determine if a number falls between two values in an if-else statement, you need to use comparison operators like  -lt (less than), -le (less than or equal to), -gt (greater than), and -ge (greater than or equal to).

Let us check a few examples.

Example-1: Basic Example

Below is the PowerShell script to check if a number is between two values like if a number is between 10 and 20:

$number = 15

if ($number -ge 10 -and $number -le 20) {
    Write-Output "The number $number is between 10 and 20."
} else {
    Write-Output "The number $number is not between 10 and 20."
}

In this example, we use the -ge (greater than or equal to) and -le (less than or equal to) operators to check if $number is within the range [10, 20].

I executed the above PowerShell script, and you can see the output in the screenshot below:

Check if a Number is Between Two Values in a PowerShell If Else Statement

Read PowerShell If-Else String Comparison

Example 2: Using Variables for Range Boundaries

In the above example, we took one fixed value, but sometimes if the value is not fixed, we can use variables.

Here is a complete script on how to use variables for the range.

$lowerBound = 10
$upperBound = 20
$number = 25

if ($number -ge $lowerBound -and $number -le $upperBound) {
    Write-Output "The number $number is between $lowerBound and $upperBound."
} else {
    Write-Output "The number $number is not between $lowerBound and $upperBound."
}

Example 3: Exclusive Ranges

If you want to check if a number is strictly between two values, excluding the boundaries. In such cases, you can use the -gt (greater than) and -lt (less than) operators.

Below is the PowerShell script.

$lowerBound = 10
$upperBound = 20
$number = 10

if ($number -gt $lowerBound -and $number -lt $upperBound) {
    Write-Output "The number $number is strictly between $lowerBound and $upperBound."
} else {
    Write-Output "The number $number is not strictly between $lowerBound and $upperBound."
}

In this example, the number 10 is not considered to be strictly between 10 and 20, so the output will indicate that it is not within the range.

Read Check if a String Contains Special Characters in PowerShell

Example 4: Validate User Input

You might find requirements where you want to validate the range after range input in PowerShell.

Here is a PowerShell script that prompts the user to enter their age, and you want to ensure the input is a valid age between 0 and 120:

$age = Read-Host "Please enter your age"

if ($age -match '^\d+$' -and [int]$age -ge 0 -and [int]$age -le 120) {
    Write-Output "You entered a valid age: $age."
} else {
    Write-Output "Invalid input. Please enter a number between 0 and 120."
}

In this example, we use the -match operator with a regular expression to ensure the input is a numeric value. We then convert the input to an integer and check if it is within the valid age range.

You can see in the screenshot below that after it asked me to enter the value, the output came.

PowerShell If Else Statement to Check if a Number is Between Two Values

Read PowerShell ForEach-Object

Example 5: Filter Data Between Two Values in If-Else Statement

Here is an example where you have a list of cities in the USA with their populations, and you want to filter out cities with populations between 100,000 and 500,000:

Below is the complete PowerShell script.

$cities = @(
    @{Name = "New York"; Population = 8419600},
    @{Name = "Los Angeles"; Population = 3980400},
    @{Name = "Chicago"; Population = 2716000},
    @{Name = "Houston"; Population = 2328000},
    @{Name = "Phoenix"; Population = 1690000},
    @{Name = "San Antonio"; Population = 1532000},
    @{Name = "San Diego"; Population = 1424000},
    @{Name = "Dallas"; Population = 1343000},
    @{Name = "Austin"; Population = 964000},
    @{Name = "Jacksonville"; Population = 911000}
)

$filteredCities = $cities | Where-Object {
    $_.Population -ge 100000 -and $_.Population -le 500000
}

$filteredCities | ForEach-Object {
    Write-Output "City: $($_.Name), Population: $($_.Population)"
}

In this example, we use the Where-Object cmdlet to filter the cities based on their population. The condition checks if the population is between 100,000 and 500,000.

Example 6: Create a Range Check Function

Let’s create a PowerShell function that checks if a number is between two values in an if-else statement and returns a boolean value:

function Is-Between {
    param (
        [int]$Number,
        [int]$LowerBound,
        [int]$UpperBound
    )

    return ($Number -ge $LowerBound -and $Number -le $UpperBound)
}

# Usage example
$number = 45
$lowerBound = 30
$upperBound = 60

if (Is-Between -Number $number -LowerBound $lowerBound -UpperBound $upperBound) {
    Write-Output "The number $number is between $lowerBound and $upperBound."
} else {
    Write-Output "The number $number is not between $lowerBound and $upperBound."
}

The above PowerShell function takes three parameters: the number to check, the lower bound, and the upper bound. It returns $true if the number is within the range and $false otherwise.

Conclusion

With all the above PowerShell examples, I hope you learn how to check whether a number is between two values in the PowerShell if-else statement.

I have also explained how to create a function to reuse the logic. Do leave a comment below if you still have questions.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.