Last active
February 11, 2017 15:23
-
-
Save Measter/0a7937609a1f1fb60269bce4c769d23f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo "List iteration:" | |
"first", "second", "I'll do it!" | foreach {"The $_ item"} | |
echo "" | |
echo "Reading values from a command:" | |
cat ".\myfile.txt" | foreach {$_} | |
echo "" | |
echo "Field separator:" | |
cat ".\myOtherFile.txt" -Delimiter ":" | foreach {$_.TrimEnd(':')} | |
echo "" | |
echo "Iterating over directory contents:" | |
ls "~" | foreach { | |
if ($_.Mode -match "d") { | |
echo "$($_.Name) is directory" | |
} else { | |
echo "$($_.Name) is file" | |
} | |
} | |
echo "" | |
echo "For loop:" | |
for ($i = 1; $i -le 10; $i++) { | |
echo "Number is $i" | |
} | |
echo "" | |
echo "While loop:" | |
$var1 = 5 | |
while ($var1 -gt 0) { | |
echo $var1 | |
$var1-- | |
} | |
echo "" | |
echo "Nested loops:" | |
for ($i = 1; $i -le 3; $i++) { | |
echo "Start ${i}:" | |
for($j = 1; $j -le 3; $j++) { | |
echo " Inner loop: $j" | |
} | |
} | |
echo "" | |
echo "Looping on File Data:" | |
cat ".\myData.txt" | foreach { | |
echo "Values in ${_} -" | |
$_ -split ':' | foreach { | |
echo " $_" | |
} | |
} | |
echo "" | |
echo "Controlling the loop: Break" | |
for ($i = 1; $i -lt 10; $i++) { | |
if ($i -eq 5) { | |
break | |
} | |
echo "Number: $i" | |
} | |
echo "" | |
echo "Controlling the loop: Continue" | |
for ($i = 1; $i -lt 15; $i++) { | |
if ($i -gt 5 -and $i -lt 10) { | |
continue | |
} | |
echo "Iteration number: $i" | |
} | |
echo "" | |
echo "Processing the output of a loop:" | |
$( for ($i = 1; $i -lt 10; $i++) { | |
echo "Number is $i" | |
}) *>&1 > done.txt | |
echo "Finished." | |
$Env:Path -split ';' | foreach { | |
echo $_ | |
ls $_ | foreach { | |
if ($_ -like "*.exe") { | |
echo " $_" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment