Help with Switch Statement

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 :

$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

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:

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.

NevGi,

welcome to the forum. :wave:t4:

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.

$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.

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.

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

So everything works as expected? … great. :+1:t4:

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

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://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

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.

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. :wink:

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

$Install = Read-Host "Choose"

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

Output I wanted was:

Choose:1,2
Choco install brave googlechrome -y

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.

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

$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"

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

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:

$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)'"
}

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