Thursday, August 18, 2016

PowerShell - Loops

# While loop
# Counts from 1 to 3
$myVar = 1
while ($myVar -le 3)
{
    Write-Host $myVar
    $myVar++
}




# Do While loop
Write-Host "Another way to count from 1 to 3"
$myVar = 1
do
{
    $myVar
    $myVar++
} while ($myVar -le 3)




# For loop
Write-Host "And another way"
for ($myVar = 1; $myVar -le 3; $myVar++ )
    {
    $myVar
    }




# Now do it with a hash
$myArray = @(1, 2, 3)
Write-Host "Geting fancier"
foreach($myVar in $myArray)
{
    Write-Host "Value $myVar"
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.