3

I want to parse user values contained in .CSV file. I don't want my users to enter "Yes" or "No" but instead enter "True" or "False". In each case I want to convert to the equivalent boolean values: $true or $false. Ideally I would like a default value, so if there's misspelt "Yes or "No" I would return my default value: $true or $false.

Hence, I wondered if there is a neat way of doing this other than

if(){} else (){}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
westerdaled
  • 231
  • 3
  • 15
  • I have problems with your 'misspelling' caveat here. So, you're saying if the user enters 'Yen' that you want it to be $true? What are the conditions? The first letter has to match? – gravity Mar 29 '16 at 16:51

8 Answers8

6

One way is a switch statement:

$bool = switch ($string) {
  'yes' { $true }
  'no'  { $false }
}

Add a clause default if you want to handle values that are neither "yes" nor "no":

$bool = switch ($string) {
  'yes'   { $true }
  'no'    { $false }
  default { 'neither yes nor no' }
}

Another option might be a simple comparison:

$string -eq 'yes'            # matches just "yes"

or

$string -match '^y(es)?$'    # matches "y" or "yes"

These expressions would evaluate to $true if the string is matched, otherwise to $false.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
5

Ah, the magic of powershell functions, and invoke expression.

function Yes { $true }
function No { $false }

$magicBool = & $answer 

Note: This is case insensitive, but will not handle misspellings

Eris
  • 7,378
  • 1
  • 30
  • 45
1

If the only possible values are "Yes" and "No" then probably the simplest way is

$result = $value -eq 'Yes'

With misspelled values and the default $false the above will do as well.

With misspelled values and the default $true this will work

$result = $value -ne 'No'
Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
  • $result = $ForcePasswordChange -ne 'No' seems to be the most elegant solution as I want the default to be $true unless the client explicit requests this. Loads of good suggestions everyone. Thanks – westerdaled Mar 29 '16 at 18:24
1

All of these are valid approaches. If you are looking for a one liner, this will validate it is an acceptable value and set to boolean true if in the 'true' value set. This will also give you a default $false value.

$result = @("true","false","yes","no") -contains $value -and @("true","yes") -contains $value

For a default $true value you would need something like so.

$result = $true

if (@("true","false","yes","no") -contains $value) {
    $result = @("true","yes") -contains $value
}
Jim
  • 692
  • 7
  • 15
0

Without a full snippet of your existing code, something like this would probably be an alternative path to take, as opposed to a string of IF statements.

NOTE: This will not handle simple 'Y' or 'N' input, but is case insensitive. So, you should be able to see 'yes' or 'YES' working, as well.

$myVar = Read-Host 'What is your answer?'
switch ($myVar)
{
  Yes {$myVarConverted = $true; break}
  True {$myVarConverted = $true; break}
  No {$myVarConverted = $false; break}
  False {$myVarConverted = $false; break}
  default {"Invalid Input"; break}
}
Write-Host $myVarConverted

Please see my additional comment on your question about the 'misspelling' caveat. That's difficult to code around without specific restrictions or requirements.

gravity
  • 2,175
  • 2
  • 26
  • 34
0

Here's the way I do Yes-No answers:

function ask-user
{
   [CmdletBinding()]
   Param(
      [Parameter(Mandatory=$true)]
      [string] $question
   )
   Process
   {  $answer = read-Host $question
      if ("yes" -match $answer) {$true}
      elseif ("no" -match $answer) {$false}
      else {ask-user $question}
   }
}

You can easily substitute true and false for yes and no.
This one is case insensitive, and will match valid abbreviations. (Y or N). In the case of misspellings, it asks again. Yeah, I could have done it without recursion, but I'm lazy.

Walter Mitty
  • 18,205
  • 2
  • 28
  • 58
0

These are great solutions above, but let me just say that this whole topic just proves the vast shortcomings of Powershell...

  [System.Convert]::ToBoolean("False") -eq $true ? 
  [System.Convert]::ToBoolean("0") -eq $true ?  

Really? Give me a f--kin break.

m1m1k
  • 1,375
  • 13
  • 14
0

For me :-

Function convert2Bool($this) { return ($("False","0","","N","No",'$False',"Off") -notcontains [string]$this) }

can adjust if you don't want $null blank-string going to $false, else fine.

SimonTi
  • 11
  • 3