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

Time limit is exhausted. Please reload CAPTCHA.

Original Theme by Schiy · Powered by WordPress