SOLVED

PromptForChoice menu not working in vertical mode.

Copper Contributor

Hello, i seek the wisdom of the community again.
I have a script with few options in a menu and it works fine this way:

 

$option1 = New-Object System.Management.Automation.Host.ChoiceDescription "&Status", "Show the current Status"
$option2 = New-Object System.Management.Automation.Host.ChoiceDescription "&Disable", "Disabled actions."
$option3 = New-Object System.Management.Automation.Host.ChoiceDescription "S&tart", "Start actios."
$option4 = New-Object System.Management.Automation.Host.ChoiceDescription "&Enable", "Enable actions."
$option5 = New-Object System.Management.Automation.Host.ChoiceDescription "&Clean", "Cleanup actions ."
$option6 = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit","Exit the script."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($option1, $option2, $option3, $option4, $option5, $option6)
$decision = $Host.UI.PromptForChoice($title, $question, $options, 0)

 

I have now more options with longer choice names and it is becoming unclear, as it is stretching the choices on two (or more) lines.
So, after some searching, i have found a solution to display the choices menu vertically:

 

$decision = $Host.UI.PromptForChoice($title, $question, $options, [int[]](0))

 

However, i have a strange issue with it. Using Enter for default choice and typing "?" with Enter is executing correctly.
Other options are not executing after typing the letter + Enter, but waiting for another choice input, until Enter is pressed without a letter.

 

$decision = $Host.UI.PromptForChoice($title, $question, $options, [int[]](0))
[S] Status
[D] Disable
[T] Start
[E] Enable
[C] Clean
[X] Exit
[?] Help
(default is "S")
Choice[0]: ?
S - Show the current Status
D - Disabled actions.
T - Start actios.
E - Enable actions.
C - Cleanup actions .
X - Exit the script.
Choice[0]: D
Choice[1]:
PS C:\temp> $decision
1

 

Can someone please tell me why does it behave like that?

Thanks in advance.

5 Replies
$decision = $Host.UI.PromptForChoice($title, $question, $options, [int[]]@())

Maybe it helps :)

@NikolinoDE 

Hello, NikolinoDE :smile: Unfortunately it did not help.

$decision = $Host.UI.PromptForChoice($title, $question, $options, [int[]]@())
[S] Status
[D] Disable
[T] Start
[E] Enable
[C] Clean
[X] Exit
[?] Help
Choice[0]: D
Choice[1]:

since "@" is used as array operator - how would it influence the action?

@RedModSK 

You are correct, the @ symbol is used to create an array in PowerShell. In this case, [int[]]@() creates an empty array of integers. This is used as the default choices parameter for the PromptForChoice method.

I ran it through the various AI's (too lazy or unable to think it through myself :)) and here are the results...maybe it helps :)

 

$option1 = New-Object System.Management.Automation.Host.ChoiceDescription "&Status", "Show the current Status"
$option2 = New-Object System.Management.Automation.Host.ChoiceDescription "&Disable", "Disabled actions."
$option3 = New-Object System.Management.Automation.Host.ChoiceDescription "S&tart", "Start actios."
$option4 = New-Object System.Management.Automation.Host.ChoiceDescription "&Enable", "Enable actions."
$option5 = New-Object System.Management.Automation.Host.ChoiceDescription "&Clean", "Cleanup actions ."
$option6 = New-Object System.Management.Automation.Host.ChoiceDescription "E&xit","Exit the script."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($option1, $option2, $option3, $option4, $option5, $option6)
$title = "Title"
$question = "Question"
$decision = $Host.UI.PromptForChoice($title, $question, $options, [int[]]@())

 

Whether that works!...I don't know either, I haven't tried it :)

 

best response confirmed by RedModSK (Copper Contributor)
Solution

@RedModSK 

 

There's two different overloads for PromptForChoice:

 

  1. The single return value definition:
    PSHostUserInterface.PromptForChoice Method (System.Management.Automation.Host) | Microsoft Learn
  2. The multiple choice return values definition:
    IHostUISupportsMultipleChoiceSelection.PromptForChoice Method (System.Management.Automation.Host) | ...

 

You're using the multiple choice definition, where while that does indeed display vertically, it requires two consecutive <Enter> to confirm no further input, as a single enter simply accepts one value before letting you specify the next value on the next line.

 

See the example below:

 

LainRobertson_0-1687176933565.png

 

I'm not aware of any way to make the single return value iteration of PromptForChoice to display vertically. That doesn't mean there isn't one, just that I'm unaware of one.

 

Cheers,

Lain

Hello Lain,
thank you for the explanation and the links. It is more clear to me now.
It explains also the behavior for the default choice and the "?" choice as they are already predefined singe values.
I will probably use some visual workaround.
I can use a the title or question section to display the vertical menu as a simple text.
Or i can spend more time and give the script a GUI with WinForms.

1 best response

Accepted Solutions
best response confirmed by RedModSK (Copper Contributor)
Solution

@RedModSK 

 

There's two different overloads for PromptForChoice:

 

  1. The single return value definition:
    PSHostUserInterface.PromptForChoice Method (System.Management.Automation.Host) | Microsoft Learn
  2. The multiple choice return values definition:
    IHostUISupportsMultipleChoiceSelection.PromptForChoice Method (System.Management.Automation.Host) | ...

 

You're using the multiple choice definition, where while that does indeed display vertically, it requires two consecutive <Enter> to confirm no further input, as a single enter simply accepts one value before letting you specify the next value on the next line.

 

See the example below:

 

LainRobertson_0-1687176933565.png

 

I'm not aware of any way to make the single return value iteration of PromptForChoice to display vertically. That doesn't mean there isn't one, just that I'm unaware of one.

 

Cheers,

Lain

View solution in original post