Conditional statements in PowerShell allow you to execute code based on certain conditions. They are fundamental in controlling the flow of your scripts and making decisions within your code. In this section, we will cover the following topics:

  1. If Statements
  2. Else and ElseIf Statements
  3. Switch Statements
  4. Practical Examples
  5. Exercises

  1. If Statements

The if statement is used to test a condition. If the condition evaluates to $true, the code block inside the if statement is executed.

Syntax

if (condition) {
    # Code to execute if condition is true
}

Example

$number = 10

if ($number -gt 5) {
    Write-Output "The number is greater than 5."
}

In this example, the condition $number -gt 5 checks if the variable $number is greater than 5. Since $number is 10, the condition is true, and the message "The number is greater than 5." is printed.

  1. Else and ElseIf Statements

The else and elseif statements provide additional conditions and actions if the initial if condition is not met.

Syntax

if (condition1) {
    # Code to execute if condition1 is true
} elseif (condition2) {
    # Code to execute if condition2 is true
} else {
    # Code to execute if none of the above conditions are true
}

Example

$number = 3

if ($number -gt 5) {
    Write-Output "The number is greater than 5."
} elseif ($number -eq 5) {
    Write-Output "The number is equal to 5."
} else {
    Write-Output "The number is less than 5."
}

In this example, the script checks multiple conditions. Since $number is 3, the first condition is false, the second condition is also false, so the else block is executed, printing "The number is less than 5."

  1. Switch Statements

The switch statement is used to simplify the process of checking multiple conditions. It is particularly useful when you have a single variable that you want to compare against multiple values.

Syntax

switch (variable) {
    value1 { # Code to execute if variable equals value1 }
    value2 { # Code to execute if variable equals value2 }
    default { # Code to execute if variable does not match any value }
}

Example

$day = "Tuesday"

switch ($day) {
    "Monday" { Write-Output "Today is Monday." }
    "Tuesday" { Write-Output "Today is Tuesday." }
    "Wednesday" { Write-Output "Today is Wednesday." }
    default { Write-Output "Today is not Monday, Tuesday, or Wednesday." }
}

In this example, the script checks the value of $day and prints the corresponding message. Since $day is "Tuesday", the message "Today is Tuesday." is printed.

  1. Practical Examples

Example 1: Checking User Input

$userInput = Read-Host "Enter a number"

if ($userInput -match '^\d+$') {
    Write-Output "You entered a valid number."
} else {
    Write-Output "Invalid input. Please enter a number."
}

Example 2: File Existence Check

$filePath = "C:\example.txt"

if (Test-Path $filePath) {
    Write-Output "The file exists."
} else {
    Write-Output "The file does not exist."
}

  1. Exercises

Exercise 1: Age Category

Write a script that asks the user for their age and prints a message based on the age category:

  • "Child" if age is less than 13
  • "Teenager" if age is between 13 and 19
  • "Adult" if age is 20 or older

Solution

$age = Read-Host "Enter your age"

if ($age -lt 13) {
    Write-Output "Child"
} elseif ($age -ge 13 -and $age -le 19) {
    Write-Output "Teenager"
} else {
    Write-Output "Adult"
}

Exercise 2: Grade Evaluation

Write a script that evaluates a student's grade and prints the corresponding letter grade:

  • "A" for 90 and above
  • "B" for 80 to 89
  • "C" for 70 to 79
  • "D" for 60 to 69
  • "F" for below 60

Solution

$grade = Read-Host "Enter the grade"

if ($grade -ge 90) {
    Write-Output "A"
} elseif ($grade -ge 80) {
    Write-Output "B"
} elseif ($grade -ge 70) {
    Write-Output "C"
} elseif ($grade -ge 60) {
    Write-Output "D"
} else {
    Write-Output "F"
}

Conclusion

In this section, we covered the basics of conditional statements in PowerShell, including if, else, elseif, and switch statements. We also provided practical examples and exercises to help reinforce the concepts. Understanding and using conditional statements effectively is crucial for controlling the flow of your scripts and making decisions based on various conditions. In the next module, we will dive into loops in PowerShell, which will further enhance your scripting capabilities.

PowerShell Course

Module 1: Introduction to PowerShell

Module 2: Basic Scripting

Module 3: Working with Objects

Module 4: Advanced Scripting Techniques

Module 5: Automation and Task Scheduling

Module 6: PowerShell Remoting

Module 7: Advanced PowerShell Features

Module 8: PowerShell and DevOps

Module 9: Best Practices and Advanced Tips

© Copyright 2024. All rights reserved