PowerShell - 07.Conditional Statements

1 - If Statement
2 - If-else Statement
3 - Else-if Statement
4 - Switch Statement


Example 1:
PS C:\ps> $a=5
PS C:\ps> if ($a -eq 5) {
>> Write-Host "$a equal to 5, correct"
>> } else
>> {
>> Write-Host "$a equal to 5, wrong"
>> }
5 equal to 5, correct


Save as script1.ps1
$a=5
if ($a -eq 5) {
Write-Host "$a equal to 5, correct"
} else
{
Write-Host "$a equal to 5, wrong"
}


Example 2:
PS C:\ps> $a=8
PS C:\ps> if ($a -eq 5) {
>> Write-Host "$a equal to 5, correct"
>> } else
>> {
>> Write-Host "$a equal to 5, wrong"
>> }
8 equal to 5, wrong


Save as script2.ps1
$a=8
if ($a -eq 5) {
Write-Host "$a equal to 5, correct"
} else
{
Write-Host "$a equal to 5, wrong"
}




Related links




Previous Post Next Post