PowerShell Switch Statement with Greater Than Conditions

Today, I will explain the PowerShell switch statement with greater than conditions. As a PowerShell developer, you might come across this situation. I will explain everything about “PowerShell switch greater than” with examples.

The PowerShell switch statement allows for concise conditional logic by evaluating a variable against multiple conditions. Using greater than conditions, you can categorize values effectively. For example, to categorize ages, you can use:

switch ($age) {
    {$_ -gt 0 -and $_ -le 18} { "Minor" }
    {$_ -gt 18 -and $_ -le 35} { "Young Adult" }
    {$_ -gt 35 -and $_ -le 50} { "Adult" }
    {$_ -gt 50} { "Senior" }
}

Greater Than Conditions in PowerShell Switch Statements

The switch statement in PowerShell is a control flow statement that allows you to compare a value against multiple conditions. It’s similar to a series of if statements but more concise and easier to read. The switch statement evaluates each condition in order and executes the corresponding block of code for the first matching condition.

Here’s the basic syntax of a switch statement in PowerShell:

switch ($variable) {
    condition1 { scriptblock1 }
    condition2 { scriptblock2 }
    default { scriptblockDefault }
}

When using greater than conditions, the syntax can be modified to include comparison operators:

switch ($variable) {
    {$_ -gt value1} { scriptblock1 }
    {$_ -gt value2} { scriptblock2 }
    default { scriptblockDefault }
}

Now, let me show you some examples of how to use greater than conditions in a switch statement in PowerShell.

Example 1: Categorize Age Groups

Suppose we have a list of ages and want to categorize them into different age groups. Here’s how we can achieve this using a switch statement with gt conditions:

$ages = 18, 25, 32, 45, 60

foreach ($age in $ages) {
    switch ($age) {
        {$_ -gt 0 -and $_ -le 18} { "Age $($age): Minor" }
        {$_ -gt 18 -and $_ -le 35} { "Age $($age): Young Adult" }
        {$_ -gt 35 -and $_ -le 50} { "Age $($age): Adult" }
        {$_ -gt 50} { "Age $($age): Senior" }
        default { "Age $($age): Unknown category" }
    }
}

In this example, we categorize ages into minors, young adults, adults, and seniors based on their values.

You can see the exact output in the screenshot below. I executed the above script using VS code.

PowerShell Switch Statement with Greater Than Conditions

Check out PowerShell Switch String Contains

Example 2: Determine Salary Brackets

Consider a scenario where we need to determine salary brackets for employees in a company based in the USA. Here’s how we can use the switch statement with greater than conditions.

$salaries = 45000, 60000, 75000, 90000, 120000

foreach ($salary in $salaries) {
    switch ($salary) {
        {$_ -gt 0 -and $_ -le 50000} { "Salary $($salary): Entry Level" }
        {$_ -gt 50000 -and $_ -le 75000} { "Salary $($salary): Mid Level" }
        {$_ -gt 75000 -and $_ -le 100000} { "Salary $($salary): Senior Level" }
        {$_ -gt 100000} { "Salary $($salary): Executive Level" }
        default { "Salary $($salary): Unknown bracket" }
    }
}

This script categorizes salaries into entry-level, mid-level, senior-level, and executive-level brackets.

Here is the output in the screenshot below:

powershell switch greater than

Read PowerShell Switch Case with Regex

Example 3: Evaluate Grades

Let me show you another example of a “PowerShell switch greater than.” Here are the complete PowerShell scripts.

$students = @(
    @{ Name = "John"; Score = 85 },
    @{ Name = "Lisa"; Score = 92 },
    @{ Name = "Michael"; Score = 78 },
    @{ Name = "Sarah"; Score = 88 }
)

foreach ($student in $students) {
    switch ($student.Score) {
        {$_ -gt 90} { "$($student.Name): Grade A" }
        {$_ -gt 80} { "$($student.Name): Grade B" }
        {$_ -gt 70} { "$($student.Name): Grade C" }
        {$_ -gt 60} { "$($student.Name): Grade D" }
        default { "$($student.Name): Grade F" }
    }
}

This script evaluates each student’s score and assigns a grade accordingly. You can see the output in the screenshot below:

Greater Than Conditions in PowerShell Switch Statements

Conclusion

The PowerShell switch statement with greater than conditions is very useful. In this tutorial, I explained how to use greater than conditions in the PowerShell switch statement with real examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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