Subscribe to Windows IT Pro
November 21, 2008 12:00 AM

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
Windows IT Pro
InstantDoc ID #100411
Rating: (0)
Downloads
100411.zip

If you refer back to the switch statement’s syntax, notice that the last line in the statement’s script block is a default clause. The code in Listing 4 uses a default clause rather than defining a third condition. Any event that doesn’t contain an EntryType value of error or warning is treated as a default, which means that the returned message will begin with Info only:. Listing 4 returns the same results as Listing 3 but with a bit less work.

Using Wildcards and Regular Expressions
By default, the string value specified in the switch statement’s script block has to exactly match a condition for that condition to evaluate to true. This would be the same as using the -exact option in a switch statement. Even though the option isn’t necessary for exact matches, a person might include it to ensure that the intent of the code is clear, should anyone else review the code.

Besides the -exact option, there are options that let you use wildcards (-wildcard option) or regular expressions (-regex option) in switch statements. (If you’re unfamiliar with wildcards or regular expressions, see the PowerShell help topics about_wildcard and about_regular_ expression.) For example, the switch statement in Listing 5 uses wildcards to move files. The first line retrieves a list of text files and assigns them to the $files variable, which becomes the collection. Notice that ($files) is preceded by the -wildcard option, which tells PowerShell that wildcards will be used.

For example, the condition in callout A in Listing 5 uses the wildcard *2007*, which means the filename must contain the string 2007, with any number of characters on either side. If a filename contains the string 2007, the switch statement moves the file to the 2007 folder and displays a message indicating that the file has been moved. Because the collection contained eight files, PowerShell returns eight messages, as shown in Figure 2.

Like Listings 3 and 4, Listing 5 uses only a variable name (e.g., $files, $event) for the collection. Unlike Listings 3 and 4, Listing 5 specifies only the $_ built-in variable in each condition and not $_.PropertyName (where PropertyName is the name of the property you want to retrieve). Sometimes the only way to know which technique will work is through trial and error.

When you want to use regular expressions, you use the -regex option. For example, the switch statement in Listing 6 uses two regular expressions, the first of which is in the condition in callout A. This condition uses the regular expression archive.._200\[3-5] .txt to delete any file whose filename begins with the string archive and ends with the string _2003.txt, _2004.txt, or _2005.txt. The condition in callout B uses the regular expression archive.._2006.txt to move any file that begins with the string archive and ends with the string _2006.txt to the 2006 folder. The default clause moves all other files to the 2007 folder. Figure 3 shows the messages outputted from the code in Listing 6.

As I mentioned previously, the switch statement supports the -casesensitive option, which lets you make the matching process case sensitive. You can use this option with other options, as Listing 7 shows. In this code, notice that I use -case instead of -casesensitive. You can use a short version of an option name if PowerShell can distinguish the correct option.

With the addition of the -casesensitive option, the filenames’ cases must exactly match. For example, the second condition (archive.._2006.txt) evaluates to true for archive05_2006.txt but not for Archive05_ 2006.txt. Because all eight files begin with uppercase, the default condition applies and all eight files are moved to the 2007 folder, as shown in Figure 4.

Working with a File’s Contents
Another useful option is -file. You use this option when you want to use a file’s contents as the collection. Each line in the file represents an element in the collection. For example, the following switch statement retrieves the Archive08_2007.txt file’s contents:

switch -regex -file `
C:\ArchivedFiles\Archive08_2007.txt
{
  "line 1\)$" { "Line 1: $_" }
  "line 2\)$" { "Line 2: $_" }
  "line 3\)$" { "Line 3: $_" }
  "line 4\)$" { "Line 4: $_" }
  default { "Other line: $_" }
}

As this example shows, you must include the file’s pathname after the keyword -file. In the switch statement’s script block, the first condition specifies that a line must end in the string line 1). If the condition evaluates to true, the phrase Line 1: is printed, followed by the line itself ($_). If none of the four conditions evaluate to true, the default clause runs, as shown in Figure 5.

Moving Forward
The switch statement is a valuable tool for working with collections and multiple conditions. You can make the statement as simple or as complex as necessary. For example, you can embed other types of flow control statements within the conditions’ script blocks. Be sure to try out various configurations and access different types of data stores to better learn how to take full advantage of all that the switch statement has to offer.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.