<<< Back to Search

Statement ::: Switch

Used to check multiple conditions.

On this Article:

  • Switch (basic)
  • Switch with Break
  • Switch with Custom condition
  • Switch with Regular Expression
  • Switch with Wildcards
  • Switch over a collection

Core Snippets

# Statement ::: Switch
# www.PowerShellExamples.com


# Switch (basic) -_-

$selection = 3
switch ($selection)
{
    1 {"Selected one."}
    2 {"Selected two."}
    3 {"Selected three."}
    4 {"Selected four."}
    3 {"Three again."}
    Default {
        "No matches"
    }
}


# Switch with Break -_-

# Use Break to avoid execute multiple actions.
$selection = "a"
switch ($selection)
{
    "a" {"Selected a."; Break}
    "b" {"Selected b."; Break}
    "c" {"Selected c."; Break}
    "d" {"Selected d."; Break}
    "c" {"c again."; Break}
    Default {
        "No matches"
    }
}


# Switch with Custom condition (script blocks) -_-

$selection = "abc"
switch ($selection)
{
    {$_ -like "*b*"} {
        "Found string $_"
    }
    Default {
        "No matches"
    }
}


# Switch with Regular Expression -_-

$confirmation = "Yes"
$proceed = $null
switch -regex ($confirmation.ToLower())
{
    "^y(es)?$" { $proceed = $true }
    "^n(o)?$" { $proceed = $false }
    default { 
        $proceed = $null 
    }
}


# Switch with Wildcards -_-

$selection = "abc"
switch -Wildcard ($selection)
{
    "*b*" {
        "Found string $_"
    }
    Default {
        "No matches"
    }
}

# Switch over a collection -_-

# Important: Have in mind that Break affect execution to all collection, not single item
$selectionArr = @(4,3,1)
switch ($selectionArr)
{
    1 {"Selected one. For Item $_"}
    2 {"Selected two. For Item $_"}
    3 {"Selected three. For Item $_"}
    4 {"Selected four. For Item $_"}
    3 {"Three again. For Item $_"}
    Default {
        "No matches"
    }
}


    

Practical Samples

# Statement ::: Switch
# www.PowerShellExamples.com


Write-Host "`r`nSwitch (basic) -_-" -ForegroundColor Magenta

# On basic Sample multiple actions can be triggered if respective condition is matched 
$selection = 3
switch ($selection)
{
    1 {"Selected one."}
    2 {"Selected two."}
    3 {"Selected three."}
    4 {"Selected four."}
    3 {"Three again."}
    Default {
        "No matches"
    }
}


Write-Host "`r`nSwitch with Break -_-" -ForegroundColor Magenta

# Use Break to avoid execute multiple actions.
#   On the sample note that C has to actions but only the first one is triggered because the break in place
$selection = "c"
switch ($selection)
{
    "a" {"Selected a."; Break}
    "b" {"Selected b."; Break}
    "c" {"Selected c."; Break}
    "d" {"Selected d."; Break}
    "c" {"c again."; Break}
    Default {
        "No matches"
    }
}


Write-Host "`r`nSwitch with Custom condition (script blocks) -_-" -ForegroundColor Magenta

# On sample below a Script block is used to contruct a customized condition within the Switch
$selection = "abc"
switch ($selection)
{
    {$_ -like "*b*"} {
        "Found string $_"
    }
    Default {
        "No matches"
    }
}


Write-Host "`r`nSwitch with Regular Expression -_-" -ForegroundColor Magenta

# Note the use of Regular Expression to create match conditions
$confirmation = "Yes"
$proceed = $null
switch -regex ($confirmation.ToLower())
{
    "^y(es)?$" { $proceed = $true }
    "^n(o)?$" { $proceed = $false }
    default { 
        $proceed = $null 
    }
}

Write-Host "Proceed $proceed"


Write-Host "`r`nSwitch with Wildcards -_-" -ForegroundColor Magenta

# Note the use of wildcard to create match conditions
$selection = "abc"
switch -Wildcard ($selection)
{
    "*b*" {
        "Found string $_"
    }
    Default {
        "No matches"
    }
}

Write-Host "`r`nSwitch over a collection -_-" -ForegroundColor Magenta

# Important: Have in mind that Break affect execution to all collection, not single item
$selectionArr = @(4,3,1)
switch ($selectionArr)
{
    1 {"Selected one. For Item $_"}
    2 {"Selected two. For Item $_"}
    3 {"Selected three. For Item $_"}
    4 {"Selected four. For Item $_"}
    3 {"Three again. For Item $_"}
    Default {
        "No matches"
    }
}
 


Result

PS C:\>

Switch (basic) -_-
Selected three.
Three again.

Switch with Break -_-
Selected c.

Switch with Custom condition (script blocks) -_-
Found string abc

Switch with Regular Expression -_-
Proceed True

Switch with Wildcards -_-
Found string abc

Switch over a collection -_-
Selected four. For Item 4
Selected three. For Item 3
Three again. For Item 3
Selected one. For Item 1