Trying to create a menu but something strange is happening

Hi,

I’m trying to create a menu where the user just select one option and if the option doesn’t exists it asks to type the option again. If the option exists, it runs the code.
The problem is, using the code below when I type “1” it runs ok. When I type “2” it goes into the if statement to show the “try again” message and then it runs the code like it was supposed to…
What I’m doing wrong? I think that is something with the two character options “15”… Maybe?

function Menu{ $Loop = $True While ($Loop){ Clear-Host Write-Host Write-Host "Management console - Office 365 and Exchange Online" -ForegroundColor Green Write-Host Write-Host "`t1) Manage distribution groups" -ForegroundColor Yellow Write-Host "`t2) Manage resources" -ForegroundColor Yellow Write-Host "`t3) Manage users" -ForegroundColor Yellow Write-Host "`t4) Verify licenses" -ForegroundColor Yellow { ....other options... } Write-Host "`t15) Quit console" -ForegroundColor Yellow Write-Host

$Option = Read-Host “Select an option [1-15]”
if (($Option -lt 1) -or ($Option -gt 15)){
Clear-Host
Write-Host
Write-Host “`tThis option doesn’t exist… Try again.” -ForegroundColor Red
Write-Host
Pause
Clear-Host
$Loop = $True
}
Switch ($Option){
1{
DGMenu
}

                               2{
                                ResourcesMenu
                                }
                               
                                { ...other options... } 
                               
                               5{
                                $Loop = $True
                                Get-PSSession | Where-Object {$_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession
                                Clear-Host
                                Set-Location C:\                                    
                                Exit
                                }

PS: I said that maybe it is because of the two characters options because on the first menu I’m using, it just have 5 options and it works ok.

The reason you are entering the block with “try again” when you enter “2” as your menu choice is because you are reading the user input as a string. It’s a little more clear what’s going on when you try it alone:

> $Option = Read-Host “Select an option [1-15]”
> 2
> $Option -gt 15
> True

Notice this returns true even though 2 is NOT greater than 15. This is because you are evaluating the input 2 as a string, not an integer. The string “2” has a greater ASCII value than the string “15”, and the resulting comparison returns true.

Try this comparison instead:

> [int]$Option -gt 15
> False

Vasken,

Thank you so much! Problem solved =]