Search code examples
powershellif-statementcomparison-operators

PowerShell Comparison Trouble, can't make compound comparisons


I am having problems making a compound comparison. I have not been successful at using -ne or -or. Below is how I think it should work:

If (($Var -ne 'Y') -or ($Var -ne 'N') {
     #Logic for error message and loop back to user promt
Else { #Go to another function }

Now when I try this, no matter what I put in, I go to my error message and since that loops back around to the user prompt it just makes an infinite loop. Even if I put in Y or N, I go to the error message.

Now I've broken that code down to the below code, and that works, but I'd much rather do a compound comparison where If the variable is neither "y" or "n", go to error (this is because inputs Y and N both progress the script to the same part of my code).

If ($Var -eq 'Y') {
     #Do action 1
}
ElseIf ($Var -eq 'N') {
     #Do action 1
}
Else {
     #Give error message and loop back to prompt
}

Solution

  • If (($Var -ne 'Y') -or ($Var -ne 'N')) {
         #Logic for error message and loop back to user promt
    Else { #Go to another function }
    

    This would never work since one of the two will always be true and you are using -or. There is literally a million ways to tackle this problem, but given what you've shown and inferring you gave a multi-step process, this is what I would do:

    $step1complete = $false
    Write-Host "Some question"
    do {
      $var = Read-Host -Prompt "Enter Y or N"
      if ($var -in @('Y','N')) {
        $step1complete = $true
      }
      else {
        Write-Host "Invalid option!"
      }
    } until ($step1complete)