HOWTO · PowerShell
Boolean Values in PowerShell
Learn how PowerShell Boolean values work, how comparisons and commands produce them, and how to use them in scripts.
On this page
PowerShell represents Boolean values with $true and $false. Comparisons and commands can produce these values, and you can combine them with logical operators or use them to control an if statement.
Boolean Values in PowerShell
A Boolean value represents one of two logical states: true or false. In PowerShell, the native Boolean literals are $true and $false. PowerShell displays them as True and False, but the displayed words are not quoted strings. For example, $true is a Boolean value, while "True" is a string. Likewise, 1 and 0 are numbers, not native Boolean values. A script can convert values explicitly when needed, but it is clearer to use Boolean literals for conditions.
Boolean values are useful because comparisons and commands can produce them, and scripts can use the result to choose what to do. Comparisons use operators such as -eq (equal) and -ne (not equal). Logical operators combine or invert Boolean expressions. The examples below show the values as PowerShell writes them.
Compare Values and Combine Conditions
Comparison operators produce a Boolean result. The following operators compare values: -eq tests equality, -ne tests inequality, -gt means greater than, -lt means less than, -ge means greater than or equal to, and -le means less than or equal to. PowerShell’s comparison operators can also compare strings; their default string comparison is case-insensitive. Use -ceq when a case-sensitive equality comparison is required.
Run this example:
$score = 84
$name = "Ava"
$hasName = $name -ne ""
$passed = ($score -ge 70) -and $hasName
$needsReview = -not $passed
Write-Output (10 -eq 10)
Write-Output (10 -ne 8)
Write-Output (10 -gt 8)
Write-Output (10 -lt 8)
Write-Output (10 -le 10)
Write-Output (10 -ge 10)
Write-Output ($true -and $false)
Write-Output ($true -or $false)
Write-Output $needsReview
if ($passed) {
Write-Output "Ava passed."
} else {
Write-Output "Ava did not pass."
}
True
True
True
False
True
True
False
True
False
Ava passed.
The first six lines demonstrate the comparison operators and their Boolean results. The logical operator -and is true only when both operands are true; -or is true when at least one operand is true. -not reverses a Boolean value, so $needsReview is false when $passed is true. Parentheses make it clear which comparisons form the combined condition. In this example, $passed stores a Boolean expression, and the if statement uses that value to select one branch.
Get Boolean Results from PowerShell Commands
Some commands return Boolean values directly. Test-Path returns $true when the specified path exists and $false otherwise. The answer depends on the current machine and the path you check; do not assume that a particular Windows directory exists on every system. For instance, assign Test-Path -Path $env:TEMP to a variable to check the temporary directory defined for the current environment.
Test-Connection can return a Boolean result when used with -Quiet. For example, $reachable = Test-Connection -ComputerName "example.com" -Count 2 -Quiet stores whether the target responded to the connection attempts. Network access, DNS, firewall rules, remote configuration, and PowerShell version can affect the result or command behavior, so a result from one machine is not a universal guarantee. Check the command’s help on the system where the script will run if version-specific behavior matters.
A command may also produce text or objects rather than a single Boolean. When using command output in a condition, be deliberate about whether the command emits one result or several; collection truthiness and pipeline behavior can affect the condition. Prefer a command option such as -Quiet when the task specifically needs a Boolean response.
Use Boolean Values in Scripts
Store a comparison or command result in a descriptively named variable when the condition will be reused. For example, $isAvailable = Test-Path -Path $env:TEMP captures a check, and if ($isAvailable) { ... } can choose a response. Combining checks is useful when an action requires every prerequisite: $isReady -and $hasPermission. Use -or when either condition is sufficient, and -not when the script should act if a condition is false.
PowerShell treats Boolean values as distinct from numeric values. Do not describe 1 as $true or 0 as $false; a particular API, file format, or external system may encode a true/false field as numbers or text, but that representation belongs to that interface. Convert or validate data at the boundary instead of relying on an implicit assumption. This distinction helps avoid surprising comparisons and makes script intent easier to read.
Conclusion
PowerShell’s $true and $false values represent Boolean states. Comparison operators such as -eq, -ne, -gt, -lt, -le, and -ge create Boolean results, while -and, -or, and -not combine or invert them. Commands including Test-Path and Test-Connection -Quiet can provide Boolean results whose meaning depends on the path or environment being checked. Assign those results to clear variables and use them in conditions to control script flow. Keep Boolean values distinct from strings and numbers, especially when exchanging data with external systems.