# Help with Switch Statement

**URL:** <https://forums.powershell.org/t/help-with-switch-statement/17140>\
**Category:** PowerShell Help\
**Created:** [July 22, 2021, 4:40pm UTC](https://forums.powershell.org/t/help-with-switch-statement/17140 "2021-07-22T16:40:18Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![NevGi](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@NevGi](https://forums.powershell.org/u/NevGi)\
**Post date:** [July 22, 2021, 4:40pm UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/1 "2021-07-22T16:40:18Z")

</div>

Hello, I’m new to Powershell Scripting and i was trying to get muliple output from switch as a single var so the script is :

```auto
$write = read-host "write"

Switch ($write) {
    0 { $write_text = 'great' }
    1 { $write_text = 'will' }
    2 { $write_text = 'to' }
    3 { $write_text = 'learn' }
    4 { $write_text = 'powershell' }
}
"i have a $write_text okay"

```

Desired Output: when i give value 0,1,2,3,4 to read-host after running the script

```auto
i have a really great will to learn

```

I got no error messages but i didn’t get the desired result too. It went well when i gave read-host a single value like just 1 which gives the output:

```auto
i have a will okay

```

But when i give read-host a multiple value like 1,2 it gives me the same result as above.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 22, 2021, 5:10pm UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/2 "2021-07-22T17:10:37Z")

</div>

NevGi,

welcome to the forum. 👋🏽

When you post code, error messages, console output or sample data please format it as code using the _preformatted text_ button ( **\</\>** ).

Thanks in advance.

You have to make some small changes to make your code work.

```auto
$write = read-host "write"

Switch ($write) {
    0 { $write_text = 'great' }
    1 { $write_text = 'will' }
    2 { $write_text = 'to' }
    3 { $write_text = 'learn' }
    4 { $write_text = 'powershell' }
}
"i have a $write_text yea"

```

I’d recommend to always read the complete help topics including the examples for the cmdlets or statements or operators you’re about to use.

---

<div class="post-metadata">

**Author:** ![NevGi](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@NevGi](https://forums.powershell.org/u/NevGi)\
**Post date:** [July 23, 2021, 3:26am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/3 "2021-07-23T03:26:42Z")

</div>

I did read the about\_switch, about\_variables, about\_read-host but i got nothing. I mean the script works if i just typed the values 0,1,2,3,4 instead of doing read-host . Which gives me my desired output.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 23, 2021, 6:15am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/4 "2021-07-23T06:15:16Z")

</div>

> [@NevGi](#):
>
> I mean the script works if i just typed the values 0,1,2,3,4 instead of doing read-host .

I didn’t get that. You edited your initial question .

> [@NevGi](#):
>
> Which gives me my desired output.

So everything works as expected? … great. 👍🏽

`Read-Host` outputs a string. When you use numbers in your switch statement like `0` or `1` or `2` and you compare it against simple strings like `"0"` or `"2"` or` "2"` PowerShell does an implicit type conversion for you - so it works as expected. But if you type `"2,4"` into the Read-Host prompt and try to compare this with your switch statement it is not possible anymore to do a type conversion into an integer and your switch staement ouputs acvtually nothing. You try to compare an integer like 0 against a string like `"2,4"` - so your switch statement actually fails. But if you didn’t close your console in between the variable `$write_text` stays the same like before and you will probably get the same output like before

---

<div class="post-metadata">

**Author:** ![NevGi](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@NevGi](https://forums.powershell.org/u/NevGi)\
**Post date:** [July 23, 2021, 8:19am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/5 "2021-07-23T08:19:28Z")

</div>

I tried looking for a way to convert the string output of read-host into int but i couldn’t find anything in web or in help files. Does this mean i cannot convert read-host output into int.  
Is there any other way to get prompt for int input so that this script could work

[In Some Other Forum](https://social.technet.microsoft.com/Forums/windowsserver/en-US/e372990b-a12a-440d-b18e-7acc634376ba/question-on-int32-and-readhost)

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 23, 2021, 8:25am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/6 "2021-07-23T08:25:11Z")

</div>

> [@NevGi](#):
>
> I tried looking for a way to convert the string output of read-host into int but i couldn’t find anything in web or in help files.

[https://www.google.com/search?q=powershell+convert+string+to+int&oq=powershell+convert+string+to+int&aqs=chrome..69i57.7335j0j1&sourceid=chrome&ie=UTF-8](https://www.google.com/search?q=powershell+convert+string+to+int&oq=powershell+convert+string+to+int&aqs=chrome..69i57.7335j0j1&sourceid=chrome&ie=UTF-8)

> [@NevGi](#):
>
> Does this mean i cannot convert read-host output into int.

No. But if you want to input more than one integer at a time with Read-Host you will have to parse the input. That would make it much more complex.

> [@NevGi](#):
>
> Is there any other way to get prompt for int input so that this script could work

It depends on your particular use case. There are other ways if you have a limited choice. Are you asking for a specific use case? If “yes” please describe it. 😉

---

<div class="post-metadata">

**Author:** ![NevGi](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@NevGi](https://forums.powershell.org/u/NevGi)\
**Post date:** [July 23, 2021, 8:33am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/7 "2021-07-23T08:33:52Z")

</div>

So What I wanted to use this script was for Chocolatey.

```auto
$Install = Read-Host "Choose"

Status ($install) {
  0 {$install_what = "brave"}
  1 {$install_what = "googlechrome"}
}
Choco install $install_what -y

```

Output I wanted was:

```auto
Choose:1,2
Choco install brave googlechrome -y

```

So that it could install multiple packages at once

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 23, 2021, 9:03am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/8 "2021-07-23T09:03:48Z")

</div>

> [@NevGi](#):
>
> So that it could install multiple packages at once

That’ll going to be a little more complex. Either you parse the input returned by Read-Host with some regex and / or string acrobatics and try to extract the numbers or - what’d be way more professional - you could create a function and use the parameter validation of PowerShell to limit the input to a given list of choices.

> **[about Functions Advanced Parameters - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-7.4&viewFallbackFrom=powershell-7.1)**
>
> Explains how to add parameters to advanced functions.

A slightly easy way would be to query one item at a time. You still could collect all items with a loop. 😉 … something like this:

```auto
$List = 'brave','chrome','edge','vivaldi'
$result =
foreach ($item in $list) {
    $UserInput = Read-Host -Prompt "`nDo you want to install '$($item)'? Please type 'y' or 'n'"
    if ($UserInput -eq 'y') {
        $item
    }
}
$InstallList = $result -join ', '
"You choose to install $InstallList"

```

---

<div class="post-metadata">

**Author:** ![NevGi](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@NevGi](https://forums.powershell.org/u/NevGi)\
**Post date:** [July 23, 2021, 9:19am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/9 "2021-07-23T09:19:27Z")

</div>

I wanted to set apps with unique codes(1 = brave, 2 = chrome and so on) so that when read-host asks me to input value i can just give unique codes to them like (1,2) an it will automatically install those apps for the PC at once.

I have to install different apps in multiple computers so i wanted to give read-host multiple codes to install apps at one go so that i dont have to stay at 1 pc to install 1 app at a time having to give y and n for each app

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [July 23, 2021, 10:33am UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/10 "2021-07-23T10:33:27Z")

</div>

> [@NevGi](#):
>
> I wanted to set apps with unique codes(1 = brave, 2 = chrome and so on) so that when read-host asks me to input value i can just give unique codes to them like (1,2) an it will automatically install those apps for the PC at once.

This kind of “_user interface_” is hard to maintain and error prone. If you’re the only one to use this stuff and if you stick to your own process it might work. But you will need to parse the input from `Read-Host` anyway.  
Assumed you use the comma as the delimiter between your choices ou can use the -split operator to get all single numbers you entered. Then you have an array you can use in a loop to install the apps you want. … something like this:

```auto
$HashTable = @{
    '1' = 'chrome'
    '2' = 'brave'
    '3' = 'edge'
    '4' = 'vivaldi'
}

$UserInput = Read-Host -Prompt "Enter one or more numbers separated by a comma!"
$Choice = $UserInput -split ','

foreach ($item in $Choice) {
    "Installation of '$($HashTable.$item)'"
}

```

---

<div class="post-metadata">

**Author:** ![NevGi](https://avatars.discourse-cdn.com/v4/letter/n/b9e5f3/32.png) [@NevGi](https://forums.powershell.org/u/NevGi)\
**Post date:** [July 23, 2021, 3:23pm UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/11 "2021-07-23T15:23:07Z")

</div>

Thanks @Olaf The new script you provided worked just as i hoped it would.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:23pm UTC](https://forums.powershell.org/t/help-with-switch-statement/17140/12 "2024-05-16T20:23:48Z")

</div>

Closed on May 16, 2024, 8:23pm UTC


