top of page
  • Writer's pictureshishir kushawaha

PowerShell Flow Control and Conditional Statements

PowerShell Flow Control and Conditional Statements are fundamental concepts that allow you to control the execution flow of your scripts based on specific conditions. They provide the flexibility to make decisions, loop through collections, and perform different actions based on varying conditions. Understanding and using these constructs are crucial for effective PowerShell scripting. In this blog post, we will explore PowerShell control statements and conditional statements, which are essential for controlling the flow of your scripts based on specific conditions. We will cover the syntax, descriptions, and examples for each of these statements.


Table of Contents:

  1. If..else Statement

  2. Switch-case Statement

  3. Foreach Statement

  4. For Statement

  5. do..while Statement

  6. While Statement

  7. ? (Where-Object) Statement

  8. % (Foreach-Object) Statement

  9. Continue and Break Statements


1. If..else Statement


Description: The if..else statement is used to execute different blocks of code based on the result of a condition. It allows you to control the flow of your script based on specific conditions.

Syntax:

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

Description: The if..else statement is used to execute different blocks of code based on the result of a condition. It allows you to control the flow of your script based on specific conditions.


Example:

$age = 25
if ($age -ge 18) {
    Write-Host "You are an adult."
} else {
    Write-Host "You are a minor."
}

2. Switch-case Statement


Description: In PowerShell, a switch statement checks a variable for equality against a list of values, known as cases. It examines the variable against each case, and when a match is found, the corresponding code block is executed.


Syntax:

switch (<expression>) {
    <value1> { # Code to execute if <expression> equals <value1> }
    <value2> { # Code to execute if <expression> equals <value2> }
    default { # Code to execute if none of the above cases match }
}

Example:

$fruit = "Apple"
switch ($fruit) {
    "Apple"  { Write-Host "It's an Apple." }
    "Banana" { Write-Host "It's a Banana." }
    default  { Write-Host "It's a different fruit." }
}

3. Foreach Statement:Syntax:

foreach ($item in $collection) {
    # Code to execute for each $item
}

Description: The foreach statement is used to iterate over elements in an array or collection. It executes a block of code for each element in the collection.

Example:

$fruits = @("Apple", "Banana", "Orange")
foreach ($fruit in $fruits) {
    Write-Host "I like $fruit."
}

4. For Statement:Syntax:

for ($i = <initial value>; $i -<operator> <condition>; $i <increment>) {
    # Code to execute in each iteration
}

Description: The for statement provides a traditional loop mechanism to iterate a specific number of times. It allows you to control the loop's initialization, condition, and increment.

Example:

for ($i = 1; $i -le 5; $i++) {
    Write-Host "Iteration $i"
}

5. Do..while Statement:Syntax:

do {
    # Code to execute at least once
} while (<condition>)

Description: The do..while statement executes a block of code at least once and then repeats it as long as a specific condition is true.

Example:

$counter = 1
do {
    Write-Host "Count: $counter"
    $counter++
} while ($counter -le 5)

6. While Statement:Syntax:

while (<condition>) {
    # Code to execute in each iteration
}

Description: The while statement repeats a block of code as long as a specific condition is true.

Example:

$counter = 1
while ($counter -le 5) {
    Write-Host "Count: $counter"
    $counter++
}

7. ? (Where-Object) Statement:Syntax:

<collection> | Where-Object { <condition> }

Description: The ? (Where-Object) statement is used to filter objects from a collection based on a specific condition.

Example:

Get-Process | Where-Object { $_.WorkingSet64 -gt 1GB }

8. % (Foreach-Object) Statement:Syntax:

<collection> | ForEach-Object {
    # Code to execute for each item in the $collection
}

Description: The % (Foreach-Object) statement is similar to foreach but is used as a cmdlet to process each object in a pipeline individually.

Example:

$numbers = 1..5
$numbers | ForEach-Object {
    Write-Host "Number: $_"
}

9. Continue and Break Statements: Syntax:

foreach ($item in $collection) {
    if (<condition>) {
        continue
    }
    # Code to execute for each $item, except when the condition is true
}
foreach ($item in $collection) {
    if (<condition>) {
        break
    }
    # Code to execute for each $item, until the condition is true
}

Description: The continue statement skips the current iteration of a loop and moves to the next iteration. The break statement exits the loop prematurely when a specific condition is met.

Example:

for ($i = 1; $i -le 10; $i++) {
    if ($i -eq 5) {
        continue
    }
    Write-Host $i
}
for ($i = 1; $i -le 10; $i++) {
    if ($i -eq 5) {
        break
    }
    Write-Host $i
}

Conclusion: PowerShell control statements and conditional statements are powerful tools that enable you to control the flow of your scripts based on various conditions. Understanding how to use these statements effectively can greatly enhance your scripting capabilities and make your code more efficient and flexible.

97 views0 comments

Recent Posts

See All

Comentários


bottom of page