Often you may want to use a switch statement in PowerShell with greater than or less than conditions to return specific values based on numeric ranges.
You can use the following basic syntax to do so:
switch($points) {
{$_ -lt 10} { "Bad" }
{$_ -in 10..15 } { "OK" }
{$_ -in 16..20 } { "Good" }
{$_ -gt 20 } { "Great" }
}
This particular example looks at the value in the $points variable and does the following:
- Returns “Bad” if points is less than 10.
- Returns “OK” if points is between 10 and 15.
- Returns “Good” if points is between 16 and 20.
- Returns “Great” if points is greater than 20.
By using the -lt, -in and -gt operators, we are able to check if a given number is less than, in a range, or greater than a specific value, respectively.
The following example shows how to use this syntax in practice.
Example: How to Use Switch Statement with Greater Than / Less Than in PowerShell
Suppose that we have a variable named $points that contains the number of points scored by a particular basketball player.
Suppose that we would like to use a switch statement in PowerShell to classify the player into one of four categories based on the following logic:
- “Bad” if they scored less than 10 points.
- “OK” if they scored between 10 and 15 points.
- “Good” if they scored between 16 and 20 points.
- “Great” if they scored greater than 20 points.
We can use the following syntax to do so:
$points = 20
switch($points) {
{$_ -lt 10} { "Bad" }
{$_ -in 10..15 } { "OK" }
{$_ -in 16..20 } { "Good" }
{$_ -gt 20 } { "Great" }
}
The following screenshot shows how to use this syntax in practice:

The switch statement returns “Good” since the $points variable contained a value between 16 and 20.
Suppose instead that we change the value of the $points variable to be 7:

The switch statement returns “Bad” since the $points variable contained a value less than 10.
Note #1: In this example we used -lt and -gt to test for “less than” or “greater than” but you could also use -lte and -gte to test for “less than or equal” or “greater than or equal” instead.
Note #2: You can find the complete documentation for the switch statement in PowerShell here.
Related Tutorials
The following tutorials explain how to perform other common tasks in PowerShell:
PowerShell: How to Use Multiple Values in Switch Statement
PowerShell: How to Use Regex in Switch Statement
PowerShell: How to Check if String Starts with Specific Value
