Book Image

Mastering Windows PowerShell Scripting

By : Brenton J.W. Blawat
Book Image

Mastering Windows PowerShell Scripting

By: Brenton J.W. Blawat

Overview of this book

Table of Contents (22 chapters)
Mastering Windows PowerShell Scripting
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Switches


Switches enable you to quickly test multiple scripting scenarios without actually writing if statements with comparison operators. Switches are the most efficient flow control commands as they can quickly funnel data into different code sections based on an item. The Switch command allows you to evaluate the contents of a single variable and execute subsequent tasks based on the value of the variable. Switches also have a default value that is used by the switch when none of the values equal any of the suggested values in the switch statement. To invoke the Switch command, you declare Switch ($variableToEvaluate). The second part of the Switch command is to declare potential values that the $variableToEvaluate could be, as shown here:

$x = "that"
Switch ($x) { 
  this { write-host "Value $x equals this." }
  that { write-host "Value $x equals that." } 
  Default { write-host "Value Doesn't Match Any Other Value" }
}

The output of this is shown in the following screenshot:

The preceding...