While working on a PowerShell script, I got a requirement to convert a string to a boolean. There are different methods for this. In this tutorial, I will explain how to convert string to boolean in PowerShell using various methods with examples.
To convert a string to a Boolean in PowerShell, you can use several methods. Direct casting with [bool]$stringValue works for simple “True” or “False” strings but can be misleading for other values. The [System.Convert]::ToBoolean($stringValue) method is more reliable for “true” and “false” strings but throws errors for invalid inputs. For safer conversions, use [bool]::TryParse($stringValue, [ref]$booleanValue), which returns a Boolean indicating success without throwing errors.
Boolean Values in PowerShell
In PowerShell, Boolean values are represented by the $true and $false constants. These values are used to control the flow of scripts and commands. For example, conditional statements like if and while rely on Boolean values to determine whether a block of code should be executed.
Convert String to Boolean in PowerShell
In many situations, you might be required to convert a string to a boolean in PowerShell. Let me show you how to do this with examples by using different methods.
Method 1: Using Direct Casting
Direct casting is one of the simplest ways to convert a string to a Boolean in PowerShell. This method works well if the string is either “True” or “False” (case-insensitive).
Here is an example.
$stringTrue = "true"
$booleanTrue = [bool]$stringTrue
Write-Output $booleanTrue # Output: True
$stringFalse = "false"
$booleanFalse = [bool]$stringFalse
Write-Output $booleanFalse # Output: FalseIn this example, the strings "true" and "false" are converted to their Boolean equivalents, $true and $false, respectively.
Read How to Convert String to Int in PowerShell?
Method 2: Using [System.Convert]::ToBoolean()
Let me show you another reliable method for converting strings to booleans in PowerShell. You can use the [System.Convert]::ToBoolean() method, which converts a string to a Boolean based on its content.
Here is an example:
$stringValue = "true"
$booleanValue = [System.Convert]::ToBoolean($stringValue)
Write-Output $booleanValue # Outputs: True
$stringValue = "false"
$booleanValue = [System.Convert]::ToBoolean($stringValue)
Write-Output $booleanValue # Outputs: FalseThis case-insensitive method will correctly handle “true” and “false”. However, it will throw an error if the string is not a valid Boolean value.
I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Read Convert String to JSON in PowerShell
Method 3: Using [bool]::TryParse()
To handle invalid strings gracefully, you can use the [bool]::TryParse() method. This method attempts to parse the string and returns a Boolean value indicating whether the conversion was successful.
Here is a complete example to understand it better.
$stringTrue = "true"
[bool]$booleanTrue = $false
[System.Boolean]::TryParse($stringTrue, [ref]$booleanTrue)
Write-Output $booleanTrue # Output: True
$stringFalse = "false"
[bool]$booleanFalse = $false
[System.Boolean]::TryParse($stringFalse, [ref]$booleanFalse)
Write-Output $booleanFalse # Output: False
# Handling invalid input
$invalidString = "notABoolean"
[bool]$booleanInvalid = $false
$success = [System.Boolean]::TryParse($invalidString, [ref]$booleanInvalid)
Write-Output $success # Output: False
Write-Output $booleanInvalid # Output: FalseIn this example, Boolean.TryParse attempts to convert the string and sets the Boolean variable accordingly. If the conversion fails, it returns false for the success status, and the Boolean variable remains unchanged.
Look at the output in the screenshot below:

Method 4: Custom Function with Conditional Logic
You can create a custom function in PowerShell to handle string-to-Boolean conversion. This function can include additional logic, such as handling different representations of Boolean values (e.g., “yes”/”no”, “1”/”0″).
Below is the complete PowerShell script.
Function ConvertTo-Bool {
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=$true)]
[string]$Value
)
switch -regex ($Value.Trim().ToLower()) {
'^(1|true|yes|on)$' { return $true }
'^(0|false|no|off)$' { return $false }
default { throw "Invalid input: '$Value'. Please use 1/0, true/false, yes/no, or on/off." }
}
}
$result = ConvertTo-Bool "true"
Write-Output $result
$result = ConvertTo-Bool "false"
Write-Output $resultThis custom function uses a switch statement to match different representations of Boolean values.
You can see the output in the screenshot below:

Conclusion
PowerShell has different methods for converting a string to a boolean value. In this tutorial, I explained how to convert a string to a boolean in PowerShell using various methods. Most of the time, I prefer to use the direct casting approach, but the most reliable method is [System.Convert]::ToBoolean() for these conversions.
You may also like the following tutorials:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.