Test for Numerous Conditions with PowerShell's switch Statement

Lesson 3 in the PowerShell 201 series explores how and when to use the switch statement and its components

Robert Sheldon

November 20, 2008

9 Min Read
ITPro Today logo in a gray background | ITPro Today


Windows PowerShell’s switch statement is a powerful language construct that lets you test for specific conditions, similar to an if statement. However, a switch statement is easier to implement when you want to evaluate numerous conditions. Let’s look at the various components that make up the switch statement and explore how to use them to automate such tasks as retrieving System event log entries and performing certain actions based on the type of entry, and moving and deleting files based on their filenames.

Creating Switch Statements
The switch statement compares one or more values to one or more conditions. For each condition that evaluates to true, the statement runs the script block associated with that condition. To better understand howa switch statement works, let’s take a look at its syntax:

switch  (){   {
Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like