212

What are the Boolean literals in PowerShell?

0

3 Answers 3

236

$true and $false.

Those are constants, though. There are no language-level literals for Booleans.

Depending on where you need them, you can also use anything that coerces to a Boolean value, if the type has to be Boolean, e.g., in method calls that require Boolean (and have no conflicting overload), or conditional statements. Most non-null objects are true, for example. null, empty strings, empty arrays and the number 0 are false.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for this concise and accurate answer. It really seems that Powershell is falling back on old mistakes here, though. Why coerce any values... especially so loosely such that anything can be compared to a boolean? But I am confused what the difference is between $true, True, and true. Eg, 7 -eq 7 yields True (not $True). Azure commands, such as az group exists -n "MyResourceGroup" yields true (note lower case). What is going on? Why so inconsistent? All of these -- $true, True, and true -- are equal to $true when performing True -eq $true, etc.
@Mike: True/False is simply the string representation of a boolean. true should never appear in PowerShell; it's the C# literal for a boolean. In your Azure example I'd guess it's a string returned from an API. $true is effectively the PowerShell "literal" (technically a constant variable) for a boolean. Coercion is common for dynamic languages that allow a range of conditions to be simplified; look at JavaScript and others. It's simply a direction of language design, not merely a mistake.
24

[bool]1 and [bool]0 also works.

2 Comments

To be precise, you can use lots of things that can be coerced to boolean in contexts where conversion is taking place, e.g. in a conditional statement. And in those cases you don't need to cast at all. if ($s.Length) { ... } is perfectly fine, for example.
For completeness, you can also produce boolean value by doing New-Object -Type Boolean for false and ! (New-Object -Type Boolean) for true
9

To add more information to already existing answers: The Boolean literals $true and $false also work as is when used as command line parameters for PowerShell scripts. For the below PowerShell script which is stored in a file named installmyapp.ps1:

param (
    [bool]$cleanuprequired
)

echo "Batch file starting execution."

Now if I've to invoke this PowerShell file from a PowerShell command line, this is how I can do it:

installmyapp.ps1 -cleanuprequired $true

OR

installmyapp.ps1 -cleanuprequired 1

Here 1 and $true are equivalent. Also, 0 and $false are equivalent.

Note: Never expect that string literal true can get automatically converted to boolean. For example, if I run the below command:

installmyapp.ps1 -cleanuprequired true

it fails to execute the script with the below error:

Cannot process argument transformation on parameter 'cleanuprequired'. Cannot convert value "System.String" to type "System.Boolean". Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.