PowerShell - Loops

2022/1/29 7:06:07

本文主要是介绍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



这篇关于PowerShell - Loops的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程