While working with CSV file I had few column in the true and false boolean value. But when I was importing csv file in PowerShell it was treating them as a string value instead of boolean in the output. Here I will explore how to convert true and false values to boolean.
Method 1
To safely use and convert string value in CSV file to boolean I am using [System.Convert] .net object and its method ToBoolean(). Below is the step by step guide on how to use it.
# If I use true or false and get the type name it shows me Boolean type. [System.Convert]::ToBoolean('true').GetType() [System.Convert]::ToBoolean('false').GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Boolean System.ValueType # If instead of true or false value if I use any other string value it fails with the error. [System.Convert]::ToBoolean('Yes') MethodInvocationException: Exception calling "ToBoolean" with "1" argument(s): "String 'Yes' was not recognized as a valid Boolean." # Here I get the boolean value accordingly. [System.Convert]::ToBoolean('true') True [System.Convert]::ToBoolean('false') False # Instead of true or false value you can use 0 for false and any other number for true [System.Convert]::ToBoolean(0) False [System.Convert]::ToBoolean(1) True [System.Convert]::ToBoolean(100) True
If I import csv file and check the member property of pass it shows the type as string. To convert that pass property I am using foreach object and using [System.Convert]::ToBoolean() method converting pass property value to boolean as it can be seen in the member information earlier and later.
$rawData = Import-Csv -Path "$PSScriptRoot\info.csv" $rawData | Get-Member -Name Pass # Name MemberType Definition # ---- ---------- ---------- # Pass NoteProperty string Pass=true $data = Import-Csv -Path "$PSScriptRoot\info.csv" | ForEach-Object { $_.Pass = [System.Convert]::ToBoolean($_.Pass) $_ } $data | Get-Member -Name Pass # Name MemberType Definition # ---- ---------- ---------- # Pass NoteProperty bool Pass=True
Method 2
This is different way and I am using [bool] .net object and Parse() method.
Download this entire script code here or it is also available on github.
# While boolean parsing you can only use true or false value only. [bool]::Parse('true').GetType() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Boolean System.ValueType [bool]::Parse('true') True [bool]::Parse('false') False # If you use any other value than true or false it will simply end up with error. [bool]::Parse(0) [bool]::Parse('Yes') MethodInvocationException: Line | 0 | [bool]::Parse('Yes') | ~~~~~~~~~~~~~~~~~~~~ | Exception calling "Parse" with "1" argument(s): "String 'Yes' was not recognized as a valid Boolean." # If you are planning to use number use 0 for false and 1 for true. [bool]0 [bool]1
Here you can parse a bool value using Select-Object with name and expression.
$rawData = Import-Csv -Path "$PSScriptRoot\info.csv" $data = $rawData | Select-Object *, @{Name='Pass_Bool'; Expression={[bool]::Parse($_.Pass)}} # Student Pass Pass_Bool # ------- ---- --------- # Roman true True # Timothy false False $data | Get-Member -Name Pass, Pass_Bool # Name MemberType Definition # ---- ---------- ---------- # Pass NoteProperty string Pass=true # Pass_Bool NoteProperty System.Boolean Pass_Bool=True
Method 3
If you don't want to use any extra coding, You can use $true or $false value inside the csv file itself. If you import csv file and check the member by default it is bool value.
$rawData = Import-Csv -Path "$PSScriptRoot\info.csv" $rawData | Get-Member -Name Pass, IsActive # Name MemberType Definition # ---- ---------- ---------- # Pass NoteProperty string Pass=true # IsActive NoteProperty bool IsActive=True
Method 4
This is one more way but not recommended. It removes $ from the text and convert to boolean using any of the .net object and method shown above.
$rawData = Import-Csv -Path "$PSScriptRoot\info.csv" $data = $rawData | ForEach-Object { #Clean the dollar sign if present, then convert $replaceDoller = $_.IsActive.replace('$','') $_.IsActive = [System.Convert]::ToBoolean($replaceDoller) $_ } # Student Pass IsActive # ------- ---- -------- # Roman true True # Timothy false False $data | Get-Member -Name Pass, IsActive # Name MemberType Definition # ---- ---------- ---------- # Pass NoteProperty string Pass=true # IsActive NoteProperty bool IsActive=True
Useful Articles
PowerShell File Renaming: How to Reorder Files Sequentially Using a CSV Map
Building a Unified Ping and Port Connectivity Report with PowerShell
High-Performance Network Auditing: Custom Ping and TCP Port Testing in PowerShell .Net Objects
Parallel Ping and Port Testing in PowerShell Using Runspaces
Get PowerShell Cmdlet Parameter Enum Values Easily
How to Run PowerShell Commands on Azure VMs Remotely
A Step-by-Step Guide to Using Enter-AzVM for Remote Administration Over SSH
How to Audit HPE iLO Active Directory Settings with PowerShell and Redfish API
How to Get Installed Software Features List Using PowerShell WMI-CIM
PowerShell Pro Tip: How to Overwrite and Update the Same Line ProgressBar
How to Capture PowerShell Trace and Debug Messages in DebugView

