$host.ui.promptforchoice Console GUI box

Welcome Forums General PowerShell Q&A $host.ui.promptforchoice Console GUI box

This topic contains 2 replies, has 2 voices, and was last updated by

5 years, 7 months ago.

  • Author
  • #10723

    Topics: 3
    Replies: 2
    Points: 0
    Rank: Member

    I have the following code:

    $t = "Location"
    $msg = "What location?"
    $CFT = New-Object System.Management.Automation.Host.ChoiceDescription "&A choice 1", "1"
    $CON = New-Object System.Management.Automation.Host.ChoiceDescription "&B choice 2", "2"
    $ELP = New-Object System.Management.Automation.Host.ChoiceDescription "&C choice 3", "3"
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($CFT, $CON, $ELP)
    $result = $host.ui.PromptForChoice($t, $msg, $options, 1)

    When I run this code in ISE, I get a GUI prompt with buttons to press for the choices.
    When I run this in the console, I get a text prompt.
    I've been searching online to see how I can get the console to give the GUI prompt and all I have found is this:
    "The $host.ui.prompt like the name says, is specific to the host implementation. PowerGui have impemented a custom host / prompt which is GUI based. The default prompt on the console is the text one that you see."
    When I run $host.ui on each (ISE and console) I see the same output.
    Is there a way to temporarily change the console to use the UI that will prompt with the GUI when I run this script?

  • #10724

    Topics: 13
    Replies: 4872
    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    The host application is responsible for implementing the prompt; the console does not do a dialog, and there's no way to make it. It implements everything as text, including prompts and progress bars.

    You'd need to go with a complete alternative, such as using the .NET Visual Basic MsgBox function. Of course, you don't get granular control over the buttons with that – so the other alternative is to use Windows Forms or WPF to build a dialog from scratch.

  • #10725

    Topics: 3
    Replies: 2
    Points: 0
    Rank: Member

    OK thanks, I'll check that out or stick with the text prompt.

The topic ‘$host.ui.promptforchoice Console GUI box’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort