WEBSITE

PowerShell for Microsoft SharePoint 2010 : Flow Control and Object Disposal - Conditional Statements

2/24/2014 12:58:13 AM

Windows PowerShell supports the conditional statement if/elseif/else, which branches execution based on a condition, and the switch statement, which can handle multiple complex conditions.

The if/elseif/else Statement

The if/elseif/else statement allows you to execute a block of code if a specified condition is met. However, the statement can also execute a block of code if a condition is not met. This statement uses comparison operators to test the condition.

The following is a simple if/elseif/else statement.

PS > $url = "http://SPServer01"
PS > if((Get-SPSiteAdministration $url).DiskUsed -gt 20MB) {
>> "Disk space used is more than 20 MB"
>> } else {
>> "Disk space used is less than 20 MB"
>> }
>>
Disk space used is less than 20 MB

In this example, we use the Get-SPSiteAdministration cmdlet to retrieve the size (the amount of disk space used) of a site collection. We then use an if/else statement to check if this size exceeds 20MB. If so, we return “Disk space used is more than 20MB.” If the condition is not met, we return “Disk space used is less than 20MB.”

The next example demonstrates the use of the elseif keyword within the if/elseif/else statement.

PS > $url = "http://SPServer01"
PS > if((Get-SPSiteAdministration $url).DiskUsed -gt 20MB) {
>> "Disk space used is more than 20 MB"
>> } elseif((Get-SPSiteAdministration $url).DiskUsed -gt 10MB) {
>> "Disk space used is more than 10 MB"
>> } else {

>> "Disk space used is less than 10 MB"
>> }
>>
Disk space used is more than 10 MB

The elseif keyword lets you introduce another condition and a corresponding additional execution branch. You can add any number of elseif keywords to an if/elseif/else statement.

The switch Statement

The switch statement is a series of if statements and is used to evaluate a condition against a number of potential matches. The switch statement matches the expression with each of the conditions, and if a match is found, an action associated with the condition is performed. If more than one condition applies, the switch statement will execute each of the applicable conditions.

The following is a simple switch statement.

PS > $a = 1
PS > switch($a) {
>> 1 { "contains one" }
>> 2 { "contains two" }
>> }
>>
contains one

In this example, we choose an action based on the value in parentheses after the switch keyword. The value is matched with each of the conditions. If a match is found, the action associated with that condition is performed.

The default comparison operator used by the switch statement is the -eq operator. It is possible to use other operators when using the switch statement, as the next example demonstrates.

PS > $url = "http://SPServer01"
PS > switch((Get-SPSiteAdministration $url).DiskUsed) {
>> {$_ -gt 20MB} { "Disk space used is more than 20 MB"; Break }
>> {$_ -gt 10MB} { "Disk space used is more than 10 MB"; Break }
>> {$_ -gt 5MB} { "Disk space used is more than 5 MB"; Break }
>> Default { "Disk space used is less than 5 MB" }
>> }
>>
Disk space used is more than 10 MB

In this example, the values in parentheses are the amount of disk space used. The value is then matched against the patterns in each clause, and if a match is found, the corresponding clause is executed. Since a Break is used at the end of each condition, the switch stops as soon as a match is made. The Default clause is used to perform an action if none of the switch values match the pattern. Notice how we use the $_ variable to reference the input object.

The switch statement supports a couple of options that you can use to control the pattern matching. By default, the switch statement is case-insensitive. You can perform a case-sensitive pattern match with the -Casesensitive option.

PS > $url = "http://SPServer01"
PS > switch -CaseSensitive ($url) {
>> "http://SPServer01" {"matches http://SPServer01"; Break }
>> "http://SPSERVER01" {"matches http://SPSERVER01"; Break }
>> default {"no match found"}
>> }
>>
matches http://SPServer01

We can also use wildcard pattern matching with the switch statement. Here is an example:

PS > $url = "http://SPServer01"
PS > switch -WildCard -CaseSensitive ($url) {
>> "http*[S]*" {"Starts with 'http' and contains a upper-case S"; Break }
>> "http*[s]*" {"Starts with 'http' and contains a lower-case s"; Break }
>> }
>>
Starts with 'http' and contains a upper-case S

In this example, first we check if the value of the variable $url starts with a 'http' and contains an uppercase S. Then we check if the value of the variable $url starts with 'http' and contains a lowercase s. Since the first pattern matches the variable, the corresponding clause is executed.

The switch statement also supports regular expressions, which let you create complex pattern matches. Here is an example:

PS > $url = "http://SPServer01"

PS > switch -regex ($url) {

>> "^(http|https):/{2}" {"match found"}

>> }

>>

match found

In this example, we test if the value of the variable $url starts with 'http' or 'https' followed by the : character and two / characters.

Other  
  •  PowerShell for Microsoft SharePoint 2010 : Variables, Arrays, and Hashtables - Hashtables in Windows PowerShell
  •  PowerShell for Microsoft SharePoint 2010 : Variables, Arrays, and Hashtables - Arrays in Windows PowerShell
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 12) - Digital Asset Management
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 11) - eDiscovery and Hold , Retention
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 10) - In-Place Records Management
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 9) - Improved Records Center Site
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 8) - Content Type Syndication
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 7) - Term Store and Term Sets
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 6) - Managed Metadata Fields
  •  Sharepoint 2010 : Administering Enterprise Content Management - Document Management (part 5) - Content Types and Columns
  •  
    Video
    Top 10
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    The latest Audi TT : New angles for TT
    Era of million-dollar luxury cars
    Game Review : Hearthstone - Blackrock Mountain
    Game Review : Battlefield Hardline
    Google Chromecast
    Keyboards for Apple iPad Air 2 (part 3) - Logitech Ultrathin Keyboard Cover for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 2) - Zagg Slim Book for iPad Air 2
    Keyboards for Apple iPad Air 2 (part 1) - Belkin Qode Ultimate Pro Keyboard Case for iPad Air 2
    Michael Kors Designs Stylish Tech Products for Women
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    Popular Tags
    Video Tutorail Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Exchange Server Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe Photoshop CorelDRAW X5 CorelDraw 10 windows Phone 7 windows Phone 8 Iphone