Implicit Boolean
A value that is not Boolean but is tested as if it were true
or false
is an implicit Boolean. For example, the following if
statement tests the output from a command:
if (Get-ChildItem c:\users\a*) {
# If statement body
}
The condition evaluates as true
when the Get-ChildItem
command finds one or more files or folders. The condition evaluates as false
when no files or folders are found.
An explicit version of the same comparison is shown here:
if ($null -ne (Get-ChildItem c:\users\c*)) {
# If statement body
}
The previous explicit statement is clearly more complex and more difficult to read.
A condition with no comparison operator implicitly evaluates to false
if it is any of the following:
$null
- An empty string
- An empty array
- The numeric value 0
A variable containing a single object, or an array containing one or more elements, and so on evaluate to true
.