Getting started with PowerShellLoopsOperatorsUsing ShouldProcessPowerShell ClassesSwitch statementWorking with ObjectsUsing existing static classesBasic Set OperationsPowerShell FunctionsSending EmailHandling Secrets and CredentialsPowershell RemotingPowerShell "Streams"; Debug, Verbose, Warning, Error, Output and InformationVariables in PowerShellCommunicating with RESTful APIsWorking with the PowerShell pipelinePowerShell Background JobsReturn behavior in PowerShellWorking with XML FilesIntroduction to PsakeUsing the progress barStringsTCP Communication with PowerShellSharePoint ModuleAliasesAutomatic VariablesEnvironment VariablesPowershell profilesEnforcing script prerequisitesUsing the Help SystemSplattingDesired State ConfigurationSigning ScriptsSecurity and CryptographyCSV parsingIntroduction to PesterModules, Scripts and FunctionsPowerShell.exe Command-LineCommon parametersParameter setsRegular ExpressionsPowerShell Dynamic ParametersWMI and CIMGUI in PowershellConditional logicURL Encode/DecodeMongoDBRunning ExecutablesError handlingHashTablesActiveDirectory modulepowershell sql queriesAutomatic Variables - part 2Package managementCmdlet NamingBuilt-in variablesCreating DSC Class-Based ResourcesPowershell ModulesPowerShell WorkflowsHow to download latest artifact from Artifactory using Powershell script (v2.0 or below)?Calculated PropertiesSpecial OperatorsAnonymize IP (v4 and v6) in text file with PowershellComment-based helpAmazon Web Services (AWS) Simple Storage Service (S3)Amazon Web Services (AWS) RekognitionPSScriptAnalyzer - PowerShell Script AnalyzerNaming ConventionsEmbedding Managed Code (C# | VB)Archive ModuleInfrastructure AutomationScheduled tasks moduleISE module

Switch statement

Other topics

Remarks:

This topic is documenting the switch statement used for branching the flow of the script. Do not confuse it with switch parameters which are used in functions as boolean flags.

Simple Switch

Switch statements compare a single test value to multiple conditions, and performs any associated actions for successful comparisons. It can result in multiple matches/actions.

Given the following switch...

switch($myValue)
{
    'First Condition'    { 'First Action' }
    'Second Condition'   { 'Second Action' }
}

'First Action' will be output if $myValue is set as 'First Condition'.

'Section Action' will be output if $myValue is set as 'Second Condition'.

Nothing will be output if $myValue does not match either conditions.

Switch Statement with Regex Parameter

The -Regex parameter allows switch statements to perform regular expression matching against conditions.

Example:

switch -Regex ('Condition')
{ 
  'Con\D+ion'    {'One or more non-digits'}
  'Conditio*$'   {'Zero or more "o"'} 
  'C.ndition'    {'Any single char.'}  
  '^C\w+ition$'  {'Anchors and one or more word chars.'} 
  'Test'         {'No match'} 
}

Output:

One or more non-digits
Any single char.
Anchors and one or more word chars.

Simple Switch With Break

The break keyword can be used in switch statements to exit the statement before evaluating all conditions.

Example:

switch('Condition')
{
  'Condition'
  {
    'First Action'
  }
  'Condition'
  {
    'Second Action'
    break
  }
  'Condition'
  {
    'Third Action'
  }
}

Output:

First Action
Second Action

Because of the break keyword in the second action, the third condition is not evaluated.

Switch Statement with Wildcard Parameter

The -Wildcard parameter allows switch statements to perform wildcard matching against conditions.

Example:

switch -Wildcard ('Condition')
{ 
    'Condition'           {'Normal match'}
    'Condit*'             {'Zero or more wildcard chars.'} 
    'C[aoc]ndit[f-l]on'   {'Range and set of chars.'}  
    'C?ndition'           {'Single char. wildcard'}
    'Test*'               {'No match'} 
}

Output:

Normal match
Zero or more wildcard chars.
Range and set of chars.
Single char. wildcard

Switch Statement with Exact Parameter

The -Exact parameter enforces switch statements to perform exact, case-insensitive matching against string-conditions.

Example:

switch -Exact ('Condition')
{ 
  'condition'   {'First Action'}
  'Condition'   {'Second Action'} 
  'conditioN'   {'Third Action'}  
  '^*ondition$' {'Fourth Action'} 
  'Conditio*'   {'Fifth Action'} 
}

Output:

First Action
Second Action
Third Action

The first through third actions are executed because their associated conditions matched the input. The regex and wildcard strings in the fourth and fifth conditions fail matching.

Note that the fourth condition would also match the input string if regular expression matching was being performed, but was ignored in this case because it is not.

Switch Statement with CaseSensitive Parameter

The -CaseSensitive parameter enforces switch statements to perform exact, case-sensitive matching against conditions.

Example:

switch -CaseSensitive ('Condition')
{ 
  'condition'   {'First Action'}
  'Condition'   {'Second Action'} 
  'conditioN'   {'Third Action'}  
}

Output:

Second Action

The second action is the only action executed because it is the only condition that exactly matches the string 'Condition' when accounting for case-sensitivity.

Switch Statement with File Parameter

The -file parameter allows the switch statement to receive input from a file. Each line of the file is evaluated by the switch statement.

Example file input.txt:

condition
test

Example switch statement:

switch -file input.txt
{ 
  'condition' {'First Action'}
  'test'      {'Second Action'} 
  'fail'      {'Third Action'}   
}

Output:

First Action
Second Action

Simple Switch with Default Condition

The Default keyword is used to execute an action when no other conditions match the input value.

Example:

switch('Condition')
{
  'Skip Condition'
  {
    'First Action'
  }
  'Skip This Condition Too'
  {
    'Second Action'
  }
  Default
  {
    'Default Action'
  }
}

Output:

Default Action

Switch Statement with Expressions

Conditions can also be expressions:

$myInput = 0

switch($myInput) {
    # because the result of the expression, 4, 
    # does not equal our input this block should not be run.
    (2+2)  { 'True. 2 +2 = 4' }

    # because the result of the expression, 0, 
    # does equal our input this block should be run.
    (2-2) { 'True. 2-2 = 0' }

    # because our input is greater than -1 and is less than 1 
    # the expression evaluates to true and the block should be run.
    { $_ -gt -1 -and $_ -lt 1 } { 'True. Value is 0' }
}

#Output
True. 2-2 = 0
True. Value is 0

Contributors

Topic Id: 1174

Example Ids: 3790,3791,3792,3793,3794,3795,3796,3797,20188

This site is not affiliated with any of the contributors.