Windows PowerShell the if statement

There are several ways to control the flow of code within your powershell scripts. The if, for, and while statements are used to define conditions and the actions to happen when the conditions are met. In this blog I’ll focus specifically on the if statement.

The if statement contains a conditional code block enclosed in parentheses (), and a script block that is enclosed in braces {}. The conditional code block specifies a condition, while the script block specifies actions. When the condition is met PowerShell responds by running the script block. When the condition is not met then Powershell responds by skipping the script block.

$files = dir c:archivedfiles*.txt
if ($files -ne $null)
{
"There are files in this folder."
write-host
}

The “$files = dir c:archivedfiles*.txt” line assigns a collection of text files to $files. Next the if statement
[ if ($files -ne $null) ] uses the $files variable in its conditional code block ($files -ne $null) to state that the variable must not be null. When .txt files are present then the conditional code block is true and powershell responds by running the script block, which displays the message “there are files in this folder”. If there were no txt files then the conditional code block would have been false therefore skipping the execution of the script block.

Sometimes you may want a specific action to happen when the conditional block evaluates to false. You can accomplish this by adding an else clause. The else clause begins with the keyword else and is followed by its own script block, which runs when none of the if statements conditional blocks prove true.

$files = dir c:archivedfiles*.doc
if ($files -ne $null)
{
"There are Word " +
"files in this folder."
write-host
}
else
{
"No Word files in this folder."
write-host
}

The else script block runs when the conditional code block equals false, resulting in “No Word files in this folder” being displayed on the screen.

There will be occasions where there are more then 2 scenarios that you want to take into account. In these situations you want to use “elseif” clauses. Each “elseif” clause contains it’s own conditional block and script block.

$files = dir c:archivedfiles*.txt
if (@files -ne $null)
{
"more then 10 files in folder."
write-host
}
elseif ($files.count -gt 7 -and $files.count -le 10)
{
"8-10 files in folder."
write-host
}
elseif ($files.count -gt 4 -and $files.count -le 7)
{
"5-7 files in folder."
write-host
}
else
{
"fewer than 5 files in folder."
write-host
}
}
else
{
"no files in folder."
write-host
}

The elseif clause is used to determine how many files are in a folder. In this example one IF statement is embedded inside another. As before the code starts by defining a collection of text files to $files. Then the outer if statement checks to see if $files is equal to null. When the outer if statement finds that $files equal null, then the else clause (else { “no files in folder.” write-host } ) script block is run which displays the message “no files in folder”. If $files does not equal null, then the inner if statements conditional code block defines 3 conditions. The first is an if condition and the other two are elseif conditions. The if statement ($files.count -gt 10) says that the number of text files must be greater than 10. If this condition is true then the message “more than 10 files in folder” is displayed. The first elseif condition ($files.count -gt 7 -and $files.count -le 10) says that the total number of files must be greater than 7 and less than 10. If this is true then the message “8-10 files in folder” is displayed by the script block. The Second elseif condition ($files.count -gt 4 -and -le 7) says that the number of text files must be greater than 4 and less than 7. If This is true then the message “5-7 files in folder” is displayed.

You can also embed a foreach statement in an if statement script block;

$files = dir c:archivedfiles*.txt
if ($files -ne $null)
{
write-host
foreach ($file in $files)
{
"the $files file is " + $file.length + " bytes."
}
write-host
}
else
{
"No files in this folder."
write-host
}

If the conditional code block equals true then the foreach statement is executed in a loop returning the text file’s size.

One thought on “Windows PowerShell the if statement

Leave a Reply