powershell and if statement

Posted on 2011-05-02
Last Modified: 2012-06-27
I have made this kind of PS script. What is wrong with it?

Depending of variable $type I would like to set value of variable $value. What is wrong with this and how I could correct it?

$type = Read-Host "Write the type"
   if ($type -eq 1) {$value -eq "A"}
     elseif ($type -eq 2) {$value -eq "B"}
     elseif ($type -eq 3) {$value -eq "C"}
     else {"You typed incorrectly"}

Kind regards,
Question by:thaapavuori
    LVL 13

    Accepted Solution

    "-eq" is only when it is a comparison operator. "=" when setting a value of an object.

    $type = Read-Host "Write the type"
       if ($type -eq 1) {$value = "A"}
         elseif ($type -eq 2) {$value = "B"}
         elseif ($type -eq 3) {$value = "C"}
         else {"You typed incorrectly"}

    LVL 12

    Expert Comment

    your using the comparison operator eq to do the assignment - try this

    $type = Read-Host "Write the type"
       if ($type -eq 1) {$value = "A"}
         elseif ($type -eq 2) {$value = "B"}
         elseif ($type -eq 3) {$value = "C"}
         else {"You typed incorrectly"}

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
    "Migrate" an SMTP relay receive connector to a new server using info from an old server.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now