Home ยป PowerShell Switch Statement

PowerShell Switch Statement

The Switch statement in PowerShell provides an approach to evaluate a variable or expression against multiple possible values, executing different code blocks based on the match criteria.

The Switch statement simplifies the process of implementing multi-way branching logic, making scripts more readable and maintainable.

Syntax of the Switch Statement

The syntax of the Switch statement in PowerShell is as follows.

switch ($variable) {
    value1 {
        # Code to execute if $variable equals value1
    }
    value2 {
        # Code to execute if $variable equals value2
    }
    default {
        # Code to execute if $variable does not match any specified value
    }
}

In this syntax:

  • $variable: represents the variable or expression to evaluate.
  • Each value block specifies a possible value to match against $variable, along with the corresponding code block to execute if the match is found.
  • The default block specifies code to execute if $variable does not match any specified value.

Let’s understand PowerShell Switch statement with practical examples.

How to Use Switch Statement in PowerShell to Process User Input

$choice = Read-Host "Enter your choice (A, B, or C)"
switch ($choice) {
    'A' {
        Write-Host "Option A selected."
    }
    'B' {
        Write-Host "Option B selected."
    }
    'C' {
        Write-Host "Option C selected."
    }
    default {
        Write-Host "Invalid choice."
    }
}

In the above PowerShell script, the $choice variable stores the user input choice value read from the host terminal. It then evaluates the conditions and execute the code block if the match is found.

Enter your choice (A, B, or C): C

Option C selected.

How to Handle File Types using Switch Statement in PowerShell

$file = "test.xlsx"
switch -Wildcard ($file) {
    '*.txt' {
        Write-Host "Text file detected."
    }
    '*.xlsx' {
        Write-Host "Excel file detected."
    }
    default {
        Write-Host "Unknown file type."
    }
}

In the above PowerShell script, the $file variable stores the file name. In the Switch statement, it uses the -WildCard parameter to check for the file ends with provided extension.

Excel file detected.

Using PowerShell Switch Statement to Process Numeric Ranges

$score = 85
switch ($score) {
    {$_ -ge 90} {
        Write-Host "Excellent"
    }
    {$_ -ge 70 -and $_ -lt 90} {
        Write-Host "Good"
    }
    {$_ -ge 50 -and $_ -lt 70} {
        Write-Host "Average"
    }
    default {
        Write-Host "Fail"
    }
}

The output of the above PowerShell script is:

Good

Cool Tip: How to use if-else statement in PowerShell!

Conclusion

I hope the above article on how to use PowerShell Switch statement is helpful to you. It allows users to implement multi-way decision-making logic efficiently, enabling the scripts to execute different actions based on varying conditions.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.