-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path00_ConditionalStatements-IfElse.ps1
More file actions
135 lines (107 loc) · 3.28 KB
/
00_ConditionalStatements-IfElse.ps1
File metadata and controls
135 lines (107 loc) · 3.28 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# The if Statement (if X is true, then do Y)
# Basic Syntax:
# ----------------------------------------------------------------------------------------------------
# if (<test1>)
# {<statement list 1>}
# [elseif (<test2>)
# {<statement list 2>}]
# [else
# {<statement list 3>}]
$a = 1
if ($a -gt 2) {
Write-Host "The value $a is greater than 2."
}
elseif ($a -eq 2) {
Write-Host "The value $a is equal to 2."
}
else {
Write-Host ("The value $a is less than 2 or" +
" was not created or initialized.")
}
# Ternary operator syntax - ONLY in PowerShell 7.0
# ----------------------------------------------------------------------------------------------------
# simplified if-else statement
# <condition> ? <if-true> : <if-false>
$path = "C:\temp"
$message = (Test-Path $path) ? "Path exists" : "Path not found"
Write-host $message
$b = 3
($b -gt 2) ? "$b is greater than 2" : "$a is equal or less than 2"
$service = Get-Service w32time
$service.Status -eq 'Running' ? (Stop-Service $service) : (Start-Service $service)
# The if Statement -> you can use all kind of comparison operators in the condition:
# ----------------------------------------------------------------------------------------------------
if ( $IsWindows -eq $true)
{
Write-Output "You are working on a Windows OS"
}
# shorter way to do the same:
if ( $IsWindows )
{
Write-Output "You are working on a Windows OS"
}
$BeersOrdered = 5
if($BeersOrdered -gt 2){
Write-Output "I'm sorry but you already orderd enough beer"
}
if($BeersOrdered -lt 1){
Write-Output "Time to order some beer"
}
$ComputerName = "DC01"
if ($ComputerName -like "DC*") {
Write-Output "Shutdown $ComputerName I think nobody needs a domain controller"
}
$a = 3
if($a -is [int]){
Write-host "$a is int data type - do something"
}
# Logical operators
# ----------------------------------------------------------------------------------------------------
# -not
$path = "X:\NonexistingPath"
if ( -not ( Test-Path -Path $path ) ){
Write-host "$path does not exist"
}
# ! operator -> ! as an alias for -not
$path = "X:\NonexistingPath"
if ( ! ( Test-Path -Path $path ) ){
Write-host "$path does not exist"
}
$c
if(!$c){
Write-host "does not exist"
}
# -and
$age = 134
if ( ($age -gt 18) -and ($age -lt 30) ){
Write-host "your age is greater than 18 and less than 30"
}
# -or
$age = 14
if ( $age -le 13 -or $age -ge 55 ){
Write-host "one of the conditions is true"
}
# PowerShell expressions in the condition statement
# ----------------------------------------------------------------------------------------------------
if(test-path C:\Windows){
Write-output "C:\Windows directory is here"
}
if(get-process -name code){
Write-output "You have vscode open how it looks like"
}
# example to check if you are connected with azure:
$context = Get-AzContext
if (!$context)
{
Connect-AzAccount
}else{
Write-host "You are already connected with Azure: $context"
}
# Examples with inputs
# ----------------------------------------------------------------------------------------------------
$name = Read-host 'Enter your name'
if($name -eq "Nathanael"){
Write-host "$name, Stop talking and go back to work"
}else{
Write-host "Hello $name, you are very welcome to learn a little bit more about PowerShell"
}