Powershell: add -range parameter to switch statement

Created on 1 Mar 2020  路  1Comment  路  Source: PowerShell/PowerShell

steps to reproduce

switch -range (1..10) {
    (1..3) { Write-Host $_ -ForegroundColor Yellow}
    (4..7) {Write-Host $_ -ForegroundColor red}
    (8..10) {Write-Host $_ -ForegroundColor cyan}
}

with this switch we can use range operator

Issue-Enhancement Resolution-Answered

Most helpful comment

@p0W3RH311

switch (1..10) {
    {$_ -in (1..3)} { Write-Host $_ -ForegroundColor Yellow}
    {$_ -in (4..7)} {Write-Host $_ -ForegroundColor red}
    {$_ -in (8..10)} {Write-Host $_ -ForegroundColor cyan}
}

>All comments

@p0W3RH311

switch (1..10) {
    {$_ -in (1..3)} { Write-Host $_ -ForegroundColor Yellow}
    {$_ -in (4..7)} {Write-Host $_ -ForegroundColor red}
    {$_ -in (8..10)} {Write-Host $_ -ForegroundColor cyan}
}
Was this page helpful?
0 / 5 - 0 ratings