Skip to the main content.

Unlocking the Power of PowerShell: Tips for Success

How to Build User Interfaces in PowerShell with Console Menus

Scripts that run unattended in the background do not need user interfaces. However, scripts can also serve as tools for tasks like conversions, report generation—you name it. Adding a user interface to such scripts can be an inviting way to let users of all levels benefit from their capabilities.

Here are some of your options:

  • Hand-crafted text-based console menus
  • Graphical user interfaces using Forms or WPF (Windows only)
  • TUIs – text-based terminal user interfaces (cross-platform)

In this article series, we’ll explore each variant and provide code samples to help you quickly evaluate each option.

Hand-Tailored Console Text Menus

When user interfaces are composed using simple text, they work in any PowerShell console on any operating system, giving you maximum design freedom—but also all the work.

For simple choice prompts, you can take advantage of PowerShell’s built-in API by using the PromptForChoice() method, which handles most of the heavy lifting.

However, making this method truly user-friendly and intuitive takes some effort. Here's a wrapper function called Get-Choice that helps you generate flexible choice prompts:


function Get-Choice 
{
  [CmdletBinding(DefaultParameterSetName="YesNo")]
  param 
  (
    [Parameter(Mandatory)]
    [string] 
    $Prompt, 

    [Parameter(Mandatory,ParameterSetName="Choices")]
    [string[]]
    $UserChoices, 

    [Parameter(ParameterSetName="YesNo")]
    [switch] 
    $YesNo,

    [Parameter(ParameterSetName="YesNoCancel")]
    [switch] 
    $YesNoCancel, 
    
    [string]
    $Title='Windows PowerShell',
    
    [int] 
    $Default=0
  )

  switch ($PSCmdlet.ParameterSetName) 
  {
    "YesNo"              { $UserChoices = "&Yes", "&No" }
    "YesNoCancel"        { $UserChoices = "&Yes", "&No", "&Cancel" }
    Default              { }
  }

  $result = $Host.UI.PromptForChoice($Title, $Prompt, $UserChoices, $Default)
  
  return [PSCustomObject]@{
    UserChoice = $result
    SelectedItem = $UserChoices[$result].Replace('&','')
  }
}

Creating text-based choice prompts can be as simple as this:


PS C:\> Get-Choice -Prompt 'Do you want to restart the computer now?'

Windows PowerShell
Do you want to restart the server?
[Y] Yes  [N] No  [?] Help (default is "Y"): n

UserChoice SelectedItem
---------- ------------
         1 No

Thanks to the parameters exposed by Get-Choice, you can easily display more options, add a title, and change the default selection.


PS C:\> Get-Choice -Prompt 'Do you want to restart the server?' -YesNoCancel -Title 'Important' -Default 2

Important
Do you want to restart the server?
[Y] Yes  [N] No  [C] Cancel  [?] Help (default is "C"):

UserChoice SelectedItem
---------- ------------
         2 Cancel

You can even create custom choice prompts of your own:


PS C:\> Get-Choice -Prompt 'Which city to connect to?' -UserChoices '&New York', 'L&ondon', 'Hanno&ver' -Title 'VPN' -Default 1

VPN
Which city to connect to?
[N] New York  [O] London  [V] Hannover  [?] Help (default is "O"): v

UserChoice SelectedItem
---------- ------------
         2 Hannover

The ampersand (“&”) sets the keyboard shortcut for your custom choice item.

PromptForChoice() is an excellent, flexible, and easy-to-use option for creating simple text-based choices that work across all PowerShell versions and operating systems—especially when wrapped in a function like Get-Choice.

Note that PromptForChoice() may behave differently in non-console hosts; for example, in PowerShell ISE, it generates graphical dialogs.

PromptForChoice() is just a simple appetizer. If you're intrigued, you can create much more sophisticated menu structures. Simply search for “PowerShell console menu function,” and you'll find plenty of ready-to-use examples, functions, and even modules that generate all kinds of text-based menus.

For even more complex text-based menus—reminiscent of the good old Norton Commander—you can use TUIs (text-based user interfaces). Since TUIs are programmed similarly to graphical user interfaces (GUIs), we'll look at GUIs first in the next part.

Good2know

Your ultimate PowerShell Cheat Sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.

The poster is available for download and in paper form.

PowerShell Poster 2023

Get your poster here!

 


Related links

 

Related posts

3 min read

Designing PowerShell GUIs with Windows Forms for Interactive Scripts

PowerShell has full access to the .NET Framework and can therefore utilize all the graphical functions available in...

3 min read

How to Build User Interfaces in PowerShell with Console Menus

Scripts that run unattended in the background do not need user interfaces. However, scripts can also serve as tools for...

6 min read

PowerShell Scripts with AST: Advanced Analysis Techniques

In the previous part, we attempted to create a tool that lists all .NET methods used in a script. While this worked...

About the author: