How to convert string to boolean in this Powershell code for Exchange Online?
Be careful with -as
. It only works when a string is 0
or 1
.
Both ("FALSE") -as [bool]
and [bool]("FALSE")
will return True
!
It's better to use
[System.Convert]::ToBoolean("FALSE")[System.Convert]::ToBoolean("False")[System.Convert]::ToBoolean(0)
Or Parse
[bool]::Parse("FALSE")[bool]::TryParse("FALSE", $outputVariable) # Will not raise an exception if the parse fails
Parse
only works with strings as parameter.
You can also use the -as
operator
-HiddenFromAddressListsEnabled ($_.HiddenFromAddressListsEnabled -as [bool])