Do
W hile
L oop
Do
U ntil
L oop
Can
r epeat
a
s et
o f
c ommands
w hile
a
c ondition
i s
m et
Can
r epeat
a
s et
o f
c ommands
u ntil
a
c ondition
i s
m et
$a=1
$a=1
Do
{ $a;
$ a++}
Do
{ $a;
$ a++}
While
( $a
– lt
1 0)
Until
( $a
– gt
1 0)
For
L oop
ForEach
-‐
L oop
T hrough
C ollection
o f
O bjects
Repeat
t he
s ame
s teps
a
s pecific
n umber
o f
t imes
Loop
t hrough
a
c ollection
o f
o bjects
For ($a=1; $a –le 10; $a++)
Foreach ($i in Get-Childitem c:\windows)
1
{$a}
{$i.name; $i.creationtime}
Switch
S tatement
If
S tatement
Another
m ethod
t o
r un
a
s pecific
s et
o f
c ode
g iven
Run
a
s pecific
s et
o f
c ode
g iven
s pecific
c onditions
specific
c onditions
$a = "white"
$a = "red"
if ($a -eq "red")
switch ($a)
{"The colour is red"}
{
elseif ($a -eq "white")
"red" {"The colour is red"}
{"The colour is white"}
"white"{"The colour is white"}
else
default{"Another colour"}
{"Another colour"}
}
Reading
F rom
a
F ile
Writing
t o
a
S imple
F ile
Use
G et-‐Content
t o
c reate
a n
a rray
o f
l ines.
T hen
l oop
Use
O ut-‐File
o r
>
f or
a
s imple
t ext
f ile
through
a rray
$a = "Hello world"
$a | out-file test.txt
$a = Get-Content "c:\servers.txt"
Or
u se
>
t o
o utput
s cript
r esults
t o
f ile
foreach ($i in $a)
{$i}
.\test.ps1 > test.txt
Writing
t o
a n
H tml
F ile
Use
C onvertTo-‐Html
a nd
>
$a = Get-Process
$a | Convertto-Html -property Name,Path,Company > test.htm
Writing
t o
a
C SV
F ile
Use
E xport-‐Csv
a nd
S elect-‐Object
t o
f ilter
o utput
$a = Get-Process
$a| Select-Object Name,Path,Company | Export-Csv -path test.csv