PowerShell if/elseif/else examples

[Pages:5]PowerShell Selection Control ? if/elseif/else statements

1

PowerShell if/elseif/else examples

$a = "Powershell"! if ($a -eq "PowerShell")!

{"Statement is True"!}! else!

{"Statement is False"!}

You can check for a NULL value

$b = "Hello" if ($b -eq $NULL)

{`B is NULL'} else

{`B is NOT NULL'}

if and elseif statements

$x = 10 if ($x -eq 20) {'$x is 20'}

elseif ($x -eq 15) {'$x is 15'}

elseif ($x -eq 10) {'$x is 10'}

else {'Value of $x not known'}

# Control selection examples # "if" statement

# PowerShell If Statement Simple Example $Number = 10 if ($Number -gt 0) {write-host "Bigger than zero"}

$Data1 = 'Apple' $Data2 = 'Red'

if ($Data1 -eq 'Apple' -AND $Data2 -eq 'Red') {'This is a red apple'}

if ($Data1 -eq 'Banana' -AND $Data2 -eq 'Red') {'This is a red pple'}

PowerShell Selection Control ? if/elseif/else statements

2

# Passing argument on command line if ($args.Length -ne 1) {

Write-Host "usage:" ` $MyInvocation.InvocationName ` "" }

# Windows PowerShell example to check 'If File Exists' $ChkFile = "C:\Windows\explorer.exe" $FileExists = Test-Path $ChkFile If ($FileExists -eq $True) {

Write-Host "Yay! explorer.exe exists" }

Comparison Operators

-eq -ne -ge -gt -lt -le -like -notlike -match -notmatch -contains -notcontains

Equal Not equal Greater than or equal

Greater than Less than

Less than or equal Wildcard comparison Wildcard comparison Regular expression comparison Regular expression comparison Containment operator Containment operator

# switch statement examples $a = 5 switch ($a) {

1 {"The color is red."} 2 {"The color is blue."} 3 {"The color is green."} 4 {"The color is yellow."} 5 {"The color is orange."} 6 {"The color is purple."} 7 {"The color is pink."} 8 {"The color is brown."} default {"The color could not be determined."} }

PowerShell Selection Control ? if/elseif/else statements

3

$a = "d14151" switch -wildcard ($a) {

"a*" {"The color is red."} "b*" {"The color is blue."} "c*" {"The color is green."} "d*" {"The color is yellow."} "e*" {"The color is orange."} "f*" {"The color is purple."} "g*" {"The color is pink."} "h*" {"The color is brown."} default {"The color could not be determined."} }

$a = "d14151" switch -wildcard ($a) {

"?14150" {"The color is red."} "?14151" {"The color is blue."} "?14152" {"The color is green."} "?14153" {"The color is yellow."} "?14154" {"The color is orange."} "?14155" {"The color is purple."} "?14156" {"The color is pink."} "?14157" {"The color is brown."} default {"The color could not be determined."} }

$a = "r14151" switch -regex ($a) {

"[a-d]" {"The color is red."} "[e-g]" {"The color is blue."} "[h-k]" {"The color is green."} "[l-o]" {"The color is yellow."} "[p-s]" {"The color is orange."} "[t-v]" {"The color is purple."} "[w-y]" {"The color is pink."} "[z]" {"The color is brown."} default {"The color could not be determined."} }

PowerShell Selection Control ? if/elseif/else statements

4

$a = "14151" switch -regex ($a) {

"\d{8}" {"The color is red."} "\d{7}" {"The color is blue."} "\d{6}" {"The color is green."} "\d{5}" {"The color is yellow."} "\d{4}" {"The color is orange."} "\d{3}" {"The color is purple."} "\d{2}" {"The color is pink."} "\d{1}" {"The color is brown."} default {"The color could not be determined."} }

PowerShell Loop examples

# For loop write-host "`nFor loop example" for ($i=1;$i-le 10;$i++) { Write-Host "$i" }

# ForEach loop write-host "`nForEach example" ForEach ($i in 1..10) { Write-Host "$i" }

# while.ps1 write-host "`nWhile loop example" $i=1 while ($i -le 10) { Write-Host $i

$i++ }

PowerShell Selection Control ? if/elseif/else statements

5

# do/while loop $env:COMPUTERNAME = Read-Host 'Please enter hostname'

Do {

$RepairReinstall = Read.Host ' 1: Repair Client 2: Re-install Client Answer'

If (RepairReinstall -eq 1) { Write-Host "Repair Started" break

}

If (RepairReinstall -eq 2) { Write-Host "Re-install Started" break

}

Else { Write-Host " [WARNING] Please select whether you'd like to repair or re-

install the client" }

} While ($true)

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download