punky
punky

Reputation: 153

Powershell PromptForChoice - how to edit descriptions?

I need to create script which will run different blocks of code depending on the user input. For this I use PromptForChoice method in PS. My code currently looks like this:

$title    = 'Disk clear'
$question = 'What do you want to remove?'
$choices  = '&Browser cache','&Temp folder'

$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)

switch ($decision)
{
    0 {'You selected Browser cache'}
    1 {'You selected Temp folder'}
    Default {}
}

When I run my script I got the following output:

Disk clear

What do you want to remove?

[B] Browser cache  [T] Temp folder  [?] Help (default is "T"):

If I input ? to get a help (to find what B and T mean) I got the:

B -
T -

My question is, how to edit descriptions for options (B & T)?

Upvotes: 5

Views: 6640

Answers (1)

Ash
Ash

Reputation: 3246

The option for choices is actually of type [System.Management.Automation.Host.ChoiceDescription] and contains an option to specify the Help Message as the second string parameter.

$title    = 'Disk clear'
$question = 'What do you want to remove?'
$Choices = @(
    [System.Management.Automation.Host.ChoiceDescription]::new("&Browser Cache", "Clear the BrowserCache")
    [System.Management.Automation.Host.ChoiceDescription]::new("&Temp Folder", "Clear the TempFolder")
)
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)

Output

Disk clear
What do you want to remove?
[T] Temp Folder  [B] Browser Cache  [?] Help (default is "B"): ?
T - Clear the TempFolder
B - Clear the BrowserCache
[T] Temp Folder  [B] Browser Cache  [?] Help (default is "B"):

Upvotes: 7

Related Questions