Table of Contents

If Loop

If Loops in Powershell work the same as within all popular languages. An If Loop is a one of the fundamental condition matching clauses that make up programming.

I the simplest form the IF part of the If Loop matches a logical condition that is set by the coder. This logical condition could be if an value matches another value or is equal to a value or if a value exists. If the logical condition is matched then the loop part contains the result of the condition. The Loop result can include any programming code to output/perform/do an action.

Why If loops are used is to add logic to an program. Without Logic, programming is a procedural event known as a simple output of program based on the sole programming code input.

If Loops help add conditioning much like a robot would think about if something is this, then it does that. Loops can be iterated many times and even nested within each other. Employing loops turns a procedural code input/output into a logical program based on conditions.

Knowing the difference makes a simple code into a program.

The basic structure of an If Loop is

If (condition) {Do stuff}

# Another explanation would be
If (test) {
"Execute when true"

The Full If Else Loop framework within Powershell is

$exists = ""
If ($exists) {
else {

The If starts the framework, the (exists true) is the condition, and contained within the parenthesis { do } is the result of the condition matching true.
If the condition matches false we are able to employ an ELSE {} clause.

An else clause results the output if an loop condition is not true.
The else clause is not mandatory but is their to add more logic to the code.

Remember an If Loop has to match True to match the after parenthesis condition otherwise it will output a failure unless an else clause is specified to return an entirely different value.
Thus giving the user a chance to escape their clause without error and continue on their code.

An full example of an If loop below determines whether a file exists and if so to perform the remainder of the conditional logic.
$CheckFile = "C:\Windows\explorer.exe"
$FileExists = Test-Path $ChkFile
If ($FileExists -eq $True) {
Write-Host "File $checkfile exists"

The If Loop supports conditional matching parameters. One such parameter is -equals or -eq. Used below the If loop tests if the string exists and output whether the string is Null or Not. Note this is different from the Boolean True or False data type.

A string object in a variable can only have 3 states, $TRUE which means exists, $FALSE which means does not exist and $NULL which means the data type is $NULL neither $TRUE or $FALSE (Neither Existing or Not existing). $NULL is a placeholder.

In the bellow the write-output command has $string encased in single quotation marks which outputs the word $string instead of outputting the contents.

$string = "Hello"
IF ($string -eq $NULL)
write-output "'$string' is NULL"
write-output "Variable '$string' is NOT NULL"

Id you were to perform the same conditional logic using the full .NET class instead you would then call the [string] objects static method of ::isnullorempty() checking if the input condition is a TRUE Boolean type value.

$exists = ""
If ([string]::isnullorempty($exists)) {