PowerShell - Loops

PowerShell - Loops

If Statement
# If statement with Zero else Statement
$Number = 16
if ($Number -gt 10)
{ Write-Host "The value $Number is gerater than 10"}

# If statement with ONE else Statement
$Number = 8
if ($Number -gt 10)
{ Write-Host "The value $Number is greater than 10"}
else
{ Write-Host "The value $Number is less than or equal to 10"}

#####################################################################
# If Statement with ZERO elseif Statement
$Number = 16
if ($Number -gt 10)
{ Write-Host "The value $Number is greater than 10"}
else
{ Write-Host "The value $Number is less than or equal to 10"}

# If Statement with ONE elseif statement
$Number = 16
if ($Number -gt 10)
{ Write-Host " Number is greater than 10"}
elseif ($Number -eq 10)
{ Write-Host " Number si equal to 10"}
else
{ Write-Host " Number is less than or equal to 10"}

# Ternary Operater (Introduced In PowerShell 7.0)
2 -eq 3 ? "This is True":"This is False"
Foreach Loop
####################################################################
#*************** PowerShell Foreach Loop ***************************
####################################################################

# Running foreach Loop as a command
$fruits = "Apple", "Orange", "KiWi"; foreach ($Item in $fruits){"This is $Item"}
# Running foreach Loop in a Script
$fruits = "Apple", "Orange", "KiWi"
foreach ($Item in $fruits)
{
"This is $Item"
}
# Using foreach against Command Output
foreach ($Service in Get-Service)
{
"This is $(($Service).displayname)"
}
While Loop
####################################################################
#*************** PowerShell While Loop ***************************
####################################################################
# Using while loop to return Items of any Array or Collection
$List = 15..18
$i = 0
While ($i -le $list.count) {$list[$i];$i++}
# Using PowerShell While Loop in Interactive way
$i = 0
$answer = Read-Host -Prompt "Write further Count ? => Yes or No"
while($answer -like "Yes"){$i;$answer = Read-Host -Prompt "Enter";$i++}

image-20220128144349183

Do Loop
####################################################################
#*************** PowerShell Do Loop ********************************
####################################################################
#*************** Do-While Loop *************************************
####################################################################

# Using do-while loop to read contents of an Array or Collection
$list = 1..8
$i = 0
do{$List[$i];$i++} while ($i -le $list.count)
# Using Do-while loop Interactively
$i = 0
do{$i;$i++;$answer = Read-Host -Prompt "Write further Count ? => Yes or No"} while($answer -like "Yes")

image-20220128145950785

#*************** Do-Until Loop *************************************
####################################################################

# Using do-until loop to read contents of an Array or Collection
$list = 1..8
$i = 0
do{$List[$i];$i++} until ($i -eq $list.count)
# Using Do-until loop Interactively
$i = 0
do{$i;$i++;$answer = Read-Host -Prompt "Write further Count ? => Yes or No"} until ($answer -like "no")
For Loop
# For Loop
# Syntax ==> for (<Init>; <Condition>; <Repeat>) {<Statement list>}
for ($i = 0;$I -le 8;$i++) {$i}

# Different Variables in Statement list
$Count = 0
for ($i = 0;$I -le 8;$i++) {"$Count Apple"; $Count++}

# Using multiple commands in init and repeat portion, Usinf multiple Conditions
for (($i = 0), ($m = 100);$I -le 100 -and $m -ge 100;$i++, $m++) {"$i is $m"}

# Some Fun
$i = 0
for(;;) {$i;$i++}

# For Loop with Interaction
for(($i = 0), ($answer = Read-Host -Prompt "Write further Count ? => Yes or No");$answer -like "Yes";$i++)
{$i;$answer = Read-Host -Prompt "Write further Count ? => Yes or No"}

image-20220128152731723

posted @ 2022-01-28 15:29  晨风_Eric  阅读(52)  评论(0编辑  收藏  举报