0
0
PowerShellscripting~5 mins

Boolean values in PowerShell

Choose your learning style9 modes available
Introduction

Boolean values help you decide if something is true or false. They are like simple yes/no answers in your scripts.

Checking if a file exists before opening it.
Deciding if a user is logged in or not.
Turning a feature on or off in a script.
Validating if a number is greater than another.
Controlling loops that run only while a condition is true.
Syntax
PowerShell
$variable = $true
$variable = $false

Boolean values in PowerShell are $true and $false.

They are case-insensitive but usually written with a dollar sign and lowercase letters.

Examples
This sets a variable $isReady to true and prints it.
PowerShell
$isReady = $true
Write-Output $isReady
This sets a variable $isComplete to false and prints it.
PowerShell
$isComplete = $false
Write-Output $isComplete
This compares if 5 is greater than 3 and stores the result (true) in $check.
PowerShell
$check = (5 -gt 3)
Write-Output $check
Sample Program

This script uses a Boolean variable $isSunny to decide what message to print.

PowerShell
$isSunny = $true
if ($isSunny) {
    Write-Output "It is sunny outside."
} else {
    Write-Output "It is not sunny outside."
}
OutputSuccess
Important Notes

Boolean values are very useful for controlling the flow of your script with if statements.

Remember, anything that is not explicitly $false or $null is treated as true in conditions.

Summary

Boolean values are $true or $false in PowerShell.

They help your script make decisions.

Use them with if statements to run code only when conditions are met.