Keeran
Keeran

Reputation: 312

Powershell always gives the statement in else as result

I have this code:

$eingabe

write-Host "Eingabe Bitte:"
Read-Host = $eingabe

if($eingabe -ge 6) {
    Write-Host "Eingabe war  gleich oder grösser als 6"
} else {
    Write-Host "Eingabe war kleiner als  6"
}

Read-Host

no matter what number i enter, it shows the else statement "Eingabe war kliener als 6"(input was less than 6). Which means no matter what i enter the statement in else will be shown as the result...

did i write something wrong ?

Thx for the answers

Upvotes: 1

Views: 50

Answers (1)

user6811411
user6811411

Reputation:

As mentioned in my comment the entry of 50 will give an odd result:

## Q:\Test\2019\04\26\SO_55869325.ps1

$eingabe = Read-Host -Prompt "Eingabe Bitte"

if($eingabe -ge 6) {
    Write-Host "Eingabe $eingabe war gleich oder grösser als 6"
} else {
    Write-Host "Eingabe $eingabe war kleiner als  6"
}

Eingabe Bitte: 50
Eingabe 50 war kleiner als  6

When doing a string comparison character by character is compared until one ends.

So 5 is compared to 6 and 5 is less.

Either explicilty cast $eingabe to [int],[decimal] or [double] or reverse the logic and let PowerShell cast $Eingabe to an int

if (6 -le $Eingabe){...

Upvotes: 1

Related Questions