PowerTip: Adding a Default Exit to a PowerShell Switch


Summary: Learn how to add a default exit condition to a Windows PowerShell switch statement to control execution of commands.


Hey, Scripting Guy! QuestionI have the following switch statement, and I want to prevent the line Write-Host “switched” from executing? How can I do this?

$a = 3

switch ($a) {

 1 { “one detected” }

 2 { “two detected” }

}

Write-Host “switched”

Hey, Scripting Guy! Answer Add an exit statement to the default switch as shown here:

$a = 3

switch ($a) {

1 { “one detected” }

2 { “two detected”}

DEFAULT { exit}

}

Write-Host “switched 

Comments (6)

  1. Anonymous says:

    @Neil

    Thanks, that example is exactly what I needed.

  2. Anonymous says:

    I agree, some context would be helpful.

  3. Neil says:

    Are there other ways to exit?  I believe I tried to construct an exit just like what is shown above into a function.  The function worked, but when I called the function against a group of objects with a foreach-object loop the exit stopped the execution of the entire loop.

  4. Tay says:

    woulnt your script exit if you do that and your option isnt in the switch statement ? whats the point of that ?

  5. Neil says:

    @Tay and NMayberry

    I had a script that only needed to run Tuesday-Saturday, so instead of fighting task scheduler I did:

    IF((get-date).dayofweek -match "Sunday|Monday"){exit}

    That way it would dive out if the day was sunday or monday.  A similar type of thing could be setup with a switch statement.

  6. Chris says:

    Maybe it is not a good idea to use the switch statement, its implementation seems to have errors. To demonstrate that on (PSVersion 5.0.10514.6) I used the following little script:

    # Test switch statement
    function doTestSwitch() {
    # setup random array
    do {
    $noelements = Get-Random -Minimum 8 -Maximum 20
    } until ($noelements -gt 7)

    $arr = @(1 .. $noelements | %{Get-Random -Minimum 0 -Maximum 100})| sort -Descending

    # generate additional number
    $tofind = Get-Random -Minimum 0 -Maximum 100
    # add it to array
    $arr = if ($tofind -notin $arr) {($arr + @($tofind)) | sort -Descending}

    # determine position of additional number
    $pos = $arr.IndexOf($tofind)

    $lastidx = ($arr.Count -1)
    #"lastidx = $lastidx"
    # Determine where additional number is in array by using SWITCH and IF_ELSEIF
    $res1 = switch ($pos) {

    0 {"$tofind at start of $arr";break}
    ($_ -lt $lastidx) {"$tofind (idx= $pos) at index=$pos in $arr";break}
    ($_ -eq $lastidx) {"$tofind at end of $arr"; break}
    default {"$tofind(idx=$_) Not found in $arr"}

    }

    $res2 = if ($pos -eq 0) {"$tofind at start of $arr"}
    elseif ($pos -lt $lastidx) {"$tofind (idx= $pos) at index=$pos in $arr"}
    elseif ($pos -eq $lastidx) {"$tofind At end of $arr"}
    else {"$tofind(idx=$_) Not found in $arr"}

    #compare results of SWITCH and IF_ELSEIF methods
    if ($res1 -ne $res2) {write-error "Error in SWITCH statement:`nSWITCH RESULT=$res1`nIF_ELSIF RESULT=$res2"}
    else {"Results between SWITCH and IF..ELSEIF RESULT agree!"}
    }

    # Run test 20 times
    1 .. 20 | %{doTestSwitch}

    Using this, correct answers become statistical when using SWITCH construct, especially when testing conditions ($_ -lt $lastidx) or ($_ -eq $lastidx) in the SWITCH statement. At the moment it seems better to use IF_ELSEIF instead of SWITCH.

Skip to main content