forked from Abhisheksinha1506/Collection-of-Useful-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathControl-the-Flow-of-PowerShell.ps1
75 lines (65 loc) · 2.07 KB
/
Control-the-Flow-of-PowerShell.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Basic If Statement
$value = 10
if($value -lt 9) { Write-Host "Value is less than 9" }
if($value -gt 9) { Write-Host "Value is greater than 9" }
if($value -lt 9) { Write-Host "Value is less than 9" } else { Write-Host "Value is greater than 9" }
# Existing If Statements
if () {
$messageOne = "Matched: This is message one"
} else {
$messageOne = "Not Matched: This is message one"
}
if () {
$messageTwo = "Matched: This is message two"
} else {
$messageTwo = "Not Matched: This is message two"
}
if () {
$messageThree = "Matched: This is message three"
} else {
$messageThree = "Not Matched: This is message three"
}
Write-Host $messageOne
Write-Host $messageTwo
Write-Host $messageThree
[PSCustomObject]@{
"messageOne" = $messageOne
"messageTwo" = $messageTwo
"messageThree" = $messageThree
}
# Ternary Operator
[PSCustomObject]@{
"messageOne" = (() ? "Matched: This is message one" : "Not Matched: This is message one")
"messageTwo" = (() ? "Matched: This is message two" : "Not Matched: This is message two")
"messageThree" = (() ? "Matched: This is message three" : "Not Matched: This is message three")
}
# Switch Statement
$value = Read-Host "Type your favorite car brand"
Switch ($value)
{
Brand1 {'You typed: Brand 1'}
Brand2 {'You typed: Brand 2'}
Brand3 {'You typed: Brand 3'}
Brand4 {'You typed: Brand 4'}
}
# Switch Statement with Default
$value = Read-Host "Type your favorite car brand"
Switch ($value)
{
Brand1 {'You typed: Brand 1'}
Brand2 {'You typed: Brand 2'}
Brand3 {'You typed: Brand 3'}
Brand4 {'You typed: Brand 4'}
default {'You did not type any brand'}
}
# Multiple Switch Statement with Default
$brand1 = Read-Host "Type your first favorite car brand"
$brand2 = Read-Host "Type your second favorite car brand"
Switch ($brand1, $brand2)
{
Brand1 {'You typed: Brand 1'}
Brand2 {'You typed: Brand 2'}
Brand3 {'You typed: Brand 3'}
Brand4 {'You typed: Brand 4'}
default {'You did not type any brand'}
}