Powershell If-Elseif-Else Conditional Statement – POFTUT

# Powershell If-Elseif-Else Conditional Statement

Powershell is a really powerful scripting language. It provides  a lot of modern useful programming language features. It also have basic features like conditional statements like `If` . In this tutorial we will look syntax and use cases of If .

If is used to check if specified condition/s are met and if the condition is met do something if not check other conditions or exit. We will look different usage types below.

# Syntax

Generic syntax of the `if` statement is like below. Other than first `if` line each line optional. There may be more than one `elseif` line

```If (condition) {Block Command}

elseIf (condition) {Block Command}

...

else {Block Command}```

# Single Condition

We will start with simplest usage type of If. We will only check a single condition. According to conditions status which is logical values `true` or `false` specified script will run or not. Now we will do an example.

In this example we will check if variable `\$a` is bigger than 5 . If \$a is bigger than 5 then we will print a message saying that \$a value is bigger than 5.

```\$a=10

if (\$a -gt 5){
Write-Host("\$a is bigger then 5")
}```

Now lets examine what this code mean.

• `\$a=10` creates a numeric variable and assigns the value 10
• `if ()`  checks the statement between brackets and is the condition is true the code between curly brackets `{ Write-Host ("\$a is bigger then 5")}` is executed if not nothing will be executed
• `\$a -gt 5` is the most important part -gt means greater then so if we try to read this statement variable a is greater then. This statement will be used as condition for if. In this example \$a is 10 and this statement is true.
LEARN MORE  What Is If Statement and Use In JavaScript, PHP, Python, Java, C/C++, C#, PowerShell, Bash Programming Languages

# Multiple Conditions

Previous example we have check single condition. But the real world consist of complex problems and we have to use complex and multiple conditions in decision making process. `If` can be used to provide multiple conditions by defining them in `else` lines.

In this example we will check variable `\$a` whether it is bigger than 10 or not.

```\$a=5

if (\$a -gt 10){
Write-Host("\$a is bigger then 10")
}
elseif (\$a -le 10){
Write-Host("\$a is lower then 10")
}```
• The if statement execution will start with first `if` line but the variable data is 5 and not bigger than 10. So this line will be skipped.
• The `if` will resume with `elseif` statement and as we see that the `(\$a -le 10)` condition will return true.
• Because of the `elseif` condition is true the code block of `elseif` will be executed. We can add more `elseif` conditions than one and all of them will have same syntax but different conditions. Here an other example which have more than one `elseif`

```\$a=5

if (\$a -gt 10){
Write-Host("\$a is bigger then 10")
}
elseif (\$a -le 10){
Write-Host("\$a is lower or equal to 10 and bigger than 0")
}
elseif (\$a -lt 0){
Write-Host("\$a is lower then 0")
}```

# Else

Up to now we have defined our conditions explicitly by using `if` or `elseif` statements. What is we do not define the condition but if the previous conditions do not match we want to execute a code block? We call this `else` statement. `Else` is put as the last statement of the whole `if` block. If none of the previous conditions are met the last `else` code block is executed.

In the example if the variable `\$a` is not positive integer we will print a warning message to the console.

```\$a=-4

if (\$a -gt 10){
Write-Host("\$a is bigger then 10")
}
elseif (\$a -ge 0){
Write-Host("\$a is lower or equal to 10 and bigger than 0")
}
else{
Write-Host("\$a is not positive integer")
}```
{"ft_lang_label":"__label__en","ft_lang_prob":0.8060652,"math_prob":0.9345235,"size":3413,"snap":"2021-43-2021-49","text_gpt3_token_len":829,"char_repetition_ratio":0.16427104,"word_repetition_ratio":0.097087376,"special_character_ratio":0.25227073,"punctuation_ratio":0.05381166,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96224767,"pos_list":[0,1,2,3,4],"im_url_duplicate_count":[null,2,null,2,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-24T09:42:32Z\",\"WARC-Record-ID\":\"<urn:uuid:791eb228-719b-432c-98d3-23b484e28901>\",\"Content-Length\":\"66734\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:058a6c12-cedc-43ed-bdc3-955ce2f5c239>\",\"WARC-Concurrent-To\":\"<urn:uuid:002cd3de-d181-4954-be9c-28b22223a35c>\",\"WARC-IP-Address\":\"172.67.141.49\",\"WARC-Target-URI\":\"https://www.poftut.com/powershell-if-elseif-else-conditional-statement/\",\"WARC-Payload-Digest\":\"sha1:Y37LBVL4NDD4OPEL7D2V4ZMEA3BKFYGT\",\"WARC-Block-Digest\":\"sha1:JDLSDWP36R5LFY6JALZENMFWHUVHOCJT\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585916.29_warc_CC-MAIN-20211024081003-20211024111003-00395.warc.gz\"}"}