Powershell For loops

For loops have three parts:

For ($1; $2; $3){

}

$1 is the initial value
$2 is the condition
$3 is what to loop

first, $1 is checked against the condition ($2) then, $3 is run and checked against the condition, if it still matches the condition, the loop continues.
if it no longer matches the condition, the loop exits.

refer to le dodgy diagram

dodgediagram

 

Broken Example Code:

 

#I couldnt figure out the factorials and it kept getting me confused and stuck so ill use addition instead. >dealwithit.gif

clear-host
$nums =@()
$array =@()

[int]$times = read-host “how many sums will you do”

For ($i=0; $i -lt $times; $i++)
{

$num1 = read-host “enter a number”
$num2 = read-host “enter a second number”

$num3 = [int]$num1 + [int]$num2

$array = @($num1,$num2,$num3)
write-host $num1″+”$num2″=”$num3
}
For ($i=0; $i -lt $times; $i++){
write-host “Nums”`t”Sum”
write-host $array[-$array.count]$array[-2] `t $array[-1]
write-host $array[$i]`t$
}

Arrays in powershell

Reference sheet for my powershell class

Arrays seem to be like small temporary databases ? 😀

 

Creating an Array:

C:\PS>$numbers = @(4,7,9,11,20)     //creates array and populates entries with data
C:\PS>write-host $numbers     //prints entire array

Number 9 can be retrieved with

C:\PS>$numbers[2]    //prints 3rd item in array (integers start at 0

similarly

C:\PS>write-host $numbers[2] “this is the second item in the array”    //includes text at the end and uses the write-host command

C:\PS>$names = @(“Martin”, “Emma”, “Alan”, “Laura”)
    //this is how words are stored into an array.

Or you can  can create an empty array and then add elements later:

C:\PS>$MoreNames = @() 
C:\PS>$MoreNames += “Emma” 
C:\PS>$MoreNames += “John” 
C:\PS>$MoreNames += “Gerald”

The size of the array can be retrieved with the commands

C:\PS>$ArrayName.count 

OR
C:\PS>$ArrayName.length

(Both these seem to return the same values)

An array can be auto populated with a number range by using double dots to tell powershell to fill in the gap. eg:

C:\PS>$range = @(10..50)

Will create an array containing the numbers 10, through to and including 50.

To retrieve the last item from the array, you can use either

C:\PS>$array[$array.Length-1]

OR

C:\PS>$array[-1]

To retrieve the FIRST item in an array, you first get the length, you substitute the [-1] with [-$array.count] . Or any variation of these to your desired item.

 To store the output from a command to an array, use cmdlets:

C:\PS>$array = Get-Process

To create an array that already has item spaces for a set amount of items, you can use:

C:\PS>$NameArray = New-Object String[] 8 
C:\PS>$IntArray = New-Object Int[] 12

Beginning Assignment:

CLS  #clears all the crap off the screen from the last thing i ran 😀

write-host “Press any key to Start”
$x = $host.UI.RawUI.ReadKey(“NoEcho,IncludeKeyDown”) #waits for a key to be pushed before continuing
write-host “”
write-host “Generating random numbers”

$Item0 = Get-Random -minimum 0 -maximum 99 #Generates a random number between 0 and 99 and stores it in $Item0
$Item1 = Get-Random -minimum 0 -maximum 99
$Item2 = Get-Random -minimum 0 -maximum 99
$Item3 = Get-Random -minimum 0 -maximum 99
$Item4 = Get-Random -minimum 0 -maximum 99
$Item5 = Get-Random -minimum 0 -maximum 99
$Item6 = Get-Random -minimum 0 -maximum 99
$Item7 = Get-Random -minimum 0 -maximum 99
$Item8 = Get-Random -minimum 0 -maximum 99
$Item9 = Get-Random -minimum 0 -maximum 99

$RandomArray = @($Item0, $Item1, $Item2, $Item3,$Item4,$Item5,$Item6,$Item7,$Item8,$Item9) #stores the randomly generated numbers into $RandomArray
write-host “”
write-host “Now all the numbers are generated and stored in the `$RandomArray` array,”
write-host “we will use a sort that puts the largest into the”
write-host “variable until its reached the end of the array.”
write-host “”
[array]::sort($RandomArray) #sorts the array ?

write-host “”
write-host “The largest number in the array is” $RandomArray[-1] #writes out the last item of $RandomArray
write-host “”

 

update: next exercise involved those freaking factorials, i am once again lost and unsure how to continue <,<

WHY DO I GET STUCK ON THESE

also, i cant get loops into my head in this stupid language, why is C/C++ easier than this ?

isn’t powershell meant to be a baby language or something?