Powershell – Basics 2

If we look more into powershell, like conditional statements and looping like (if..else, switch, while) just like in .Net basically syntax looks similar to c#

#----------------------------
# Conditional statements
#---------------------------

Clear-Host
#Condition - if/else
$var = 2
if ($var -eq 1)
{
Clear-Host
"If loop"
}
else
{
clear-host
"else loop"
}

# if elseif
if($var -eq 1)
{
 clear-host
 "If -eq 1 loop"
}
else
{
    if ($var -eq 2)
    {
    clear-host
    "If -eq 2 loop"
    }
    else
    {
    clear-host
    "else else loop"
    }
}

#Switch statement for multiple conditions
clear-host
$var = 43 #Also test with 43 and 49

switch ($var)
{
    41 {"Forty one"}
    43 {"Forty three"}
    49 {"Forty nie"}
    default {"Fifty"}
}


#Will match all lines that match
clear-host
$var = 42
switch($var)
{
    42 {"Forty two"}
    "42" {"Forty Two string"}
    default {"default"}
}

#To Stop processing once a block is found use break
clear-host
$var = 42
switch($var)
{
    42 {"Forty two";break}
    "42" {"Forty Two string";break} 
    default {"default"}
}

#Swtich works with collections, looping and execyting for each match
clear-host
switch (3,1,2,42)
{ 
1 {"One"}
2 {"Two"}
3 {"Three"}
default {"The default answer"}
}

# String compares are case insensitive by default
clear-host
switch ("Some")
{
    "some" {"lowercase"}
    "SOME" {"UpperCase"}
    "Some" {"Mixedcase"}
}

# Use the -casesensitive switch to make it sensitive
clear-host
switch -casesensitive ("Some")
{
  "some" {"lowercase"}
    "SOME" {"UpperCase"}
    "Some" {"Mixedcase"}
}

# Supports wildcards
clear-host
switch -Wildcard ("Some")
{
  "so*" {"*"}
    "?ome" {"?"}
    "So??" {"???"}
}

# Note it will also support regex matches

##

#-----------------------------------------------------------
# Looping
#-----------------------------------------------------------

#while
clear-host
$i = 1
while ($i -le 5)
{
    "`$i = $i"
    $i = $i +1
}

# won't execute if condition is already true
clear-host
$i = 6
while ($i -le 5)
{
    "`$i = $i"
    $i = $i +1
}


#Do
clear-host
$i = 1
do
{
    "`$i = $i"
    $i++
}while ($i -le 5)

# Do will always execute at least once
clear-host
$i = 6
do
{
    "`$i = $i"
    $i++
}while ($i -le 5)

# Use until to make the check more positive
clear-host
$i = 1
do
{
    "`$i = $i"
    $i++
}until ($i -gt 5)

# For loop iterate a number of times
clear-host
for ($f =0;$f -le 5;$f++)
{
    "`$f = $f"
}

#Note the initializer can be set seperately
clear-host
$f=1
for (;$f -le 5;$f++)
{
    "`$f = $f"
}

# Iterating over a collection 1 by 1
clear-host
$array = 11,12,13,14,15  # Simple array
for ($f =0;$f -lt $array.Length ;$f++)
{
    "`$array[$f] =" +$array[$f]
}

# foreach works on a collection
clear-host
$array = 11,12,13,14,15  # Simple array
foreach ($item in $array)
{
    "`$item = $item"
}

# foreach works with an array of objects
clear-host
$array = 11,12,13,14,15  # Simple array
foreach ($item in Get-ChildItem)
{
  $item.Name
}

# Combine with if to give a better focus
clear-host
set-location "C:\Users\kpatil\Documents\Powershell_Tut"
$array = 11,12,13,14,15  # Simple array
foreach ($item in Get-ChildItem)
{
    if($item.Name -like "*.ps1")
    {
        $item.Name
     }
}

# Use break to get out of the loop
clear-host
set-location "C:\Users\newuser\Documents\Powershell_basics"
$array = 11,12,13,14,15  # Simple array
foreach ($item in Get-ChildItem)
{
    if($item.Name -like "*.ps1")
    {
        $item.Name
        break # Exits the loop on first hit
     }
}

# Use continue to skip the rest of a loop but got onto the next iteration
clear-host
set-location "C:\Users\newuser\Documents\Powershell_Tut"
foreach ($item in Get-ChildItem)
{
    if($item.Name -like "*.ps1")
    {
        $item.Name
        continue # 3xits the loop on first hit
     }
     "This isn't a powershell command: $item"
}

# When used in a nested loop, break exits to the outer loop
clear-host
foreach ($outside in 1..5)
{
"`$outside = $outside"
    foreach($inside in 6..9)
    {
        "   `$inside = $inside"
        break
    }
}

# Use loop labels to break to a certain loop
clear-host
:outsideloop foreach ($outside in 1..5)
{
"`$outside = $outside"
    foreach($inside in 6..9)
    {
        "   `$inside = $inside"
        break outsideloop
    }
}

# Using continuew inside an inner loop
clear-host
foreach ($outside in 1..5)
{
"`$outside = $outside"
    foreach($inside in 6..9)
    {
        "   `$inside = $inside"
        continue
        "this will never execute as continue goes back to start of inner for loop"
        #note, because we continue to the inside loop, the above line
        # will never run but it will go through all itereations of the inner loop
    }
}

clear-host
:outsideloop foreach ($outside in 1..5)
{
"`$outside = $outside"
    foreach($inside in 6..9)
    {
        "   `$inside = $inside"
        continue outsideloop
        "this will never execute as continue goes back to start of inner for loop"
        #here, because we break all the way to the outer loop the last two
        # iterations (8 and 9) never run
    }
    "this will never run too"
}

##

Leave a Reply

Your email address will not be published. Required fields are marked *