PowerShell Tutorial the switch statement part 1.

PowerShell’s switch statement is similar to an if statement though easier to implement when you want to evaluate numerous conditions. You can use the switch statement to automate tasks such as retrieving System event log entries and performing actions based on the type of entry, and or moving and deleting files based on their file names.

Simply put, the switch statement compares one or more values to one or more conditions. for each condition that prooves true, the statement runs the associated script block.

switch <options> (<collection>)
{
<condition 1> {<script block 1>}
[<condition 2> {<script block 2>}]
[<condition 3> {<script block 3>}]
[<condition n> {<script block n>}]
}

To explain the structure a bit, The first line begins with the keyword switch which then is followed by one or more options and a collection. This switch statement is then followed and supported by 5 options that allow for the use of wildcards and regular expressions. The collection, which is enclosed in parentheses, contains what you’re checking and can contain zero or more elements.
The braces found in the second and last lines, enclose the switch statement’s script block. The first line in this block “<condition 1> {<script block 1>}” includes a condition and that condition’s script block, which is also enclosed in braces. If the condition equals true, then the condition’s script block runs. There can be as many condition/script blocks as is necessary. You can also include an optional default clause that contains a script block, which will run only when none of the conditions equal true.

$event = Get-EventLog “system” -Newest 1
switch($event.EntryType)
{
“error”
{
“ERROR: ” + $event.Message
}
“warning”
{
“WARNING: ” + $event.Message
}
“information”
{
“Info only: ” + $event.Message
}
}

This example defines the $event variable and uses that variable in a switch statement. The first line uses the Get-EventLog cmdlet to retrieve the most recent event from the local computer’s System event log. The Get-EventLog cmdlet assigns the information from the most recent event to the $event variable. Using the EventLogEntry object’s EntryType property, the switch statement retrieves the type of event and compares that element to the defined conditions. Keep in mind that by default the switch statement is not case sensitive, you can override this though by specifying the -casesensitive option such as;

switch -casesensitive ($event.EntryType)

The code in our example retrieves the EntryTpe property value as part of the collection. However you can retrieve that value in the conditions as well.

$event = Get-EventLog “system” -Newest 1
switch ($event)
{
{$_.EntryType -eq “error”}
{
“ERROR: ” + $_.message
}
{$_.EntryType -eq “warning”}
{
“WARNING: ” + $_.message
}
{$_.EntryType -eq “information”}
{
“Info only: ” + $event.message
}
}

Now the collection includes only $event. The conditions themselves use the $_built-in variable to reference the current $event value, then use the EntryType property to retrieve the entry type. When using this approach you must define the entire condition and enclose it in braces. When working with a collection that contains one element, you’ll probably want to use the first approach. If you need to work with a collection that contains multiple elements though, you’ll have to use the second approach if the switch statement can’t work with the collection as is.

I’ll continue with part two “using wildcards and regular expressions” next week.

One thought on “PowerShell Tutorial the switch statement part 1.
  1. Pingback: Episode 56 - James Kovacs talks about PSake « PowerScripting Podcast

Leave a Reply