Powershell: A text based menu


a long time ago I wrote a Menu function for quickly choose predefined options in a script. An example

Source the menu.ps1 in your script and call  fShowMenu. This will show the menu. The function needs 2 parameters:
Parameter 1: The menu title as string
Parameter 2: A hashtable with the Menuentries. The key of an Menuitem is returned by the function the corresponding value is shown at the menu.

# Source the file
. .\menu.ps1
# Call Menu funtion
fShowMenu "Choose your favorite Band" @{"sl"="Slayer";"me"="Metallica";"ex"="Exodus";"an"="Anthrax"}
A text based Powershell Menu

A text based Powershell Menu

Now you can navigate through the menu by pressing the up and down arrow keys and the return key to quit the menu or you can select an menu entry directly by pressing the leading number or character of the menu item at the keyboard.

PS D:\> fShowMenu "Choose your favorite Band" @{"sl"="Slayer";"me"="Metallica";"ex"="Exodus";"an"="Anthrax"}
 Choose your favorite Band
  1: Metallica
  2: Exodus
  3: Anthrax
  4: Slayer
  Your choice: 3

In this example “3” selects “Anthrax” and the function returns “an”.

Up to 90 menu items are possible. This should be enough for the most cases.


Advertisment to support michlstechblog.info

A text based menu for powershell
A text based menu for powershell
Version: V1.0
1.6 KiB

Add a comment »2 comments to this article

  1. Where does the 90 item limit come from?


    • The limit comes from the keyboard shortcut. You have only to press one key without to hit return to select an entry and therefore only a-z, A-Z, 0-9 is available.



Time limit is exhausted. Please reload CAPTCHA.

Original Theme by Schiy · Powered by WordPress