9

I want do provide the user with a menu in Powershell where he can select entities with the arrow keys just like in GRUB or just the very basic idea of curses to replace the (Y)es or (N)o command line inputs. (Or for folder selection or whatever)

Is there already a framework or something that can help me as a starting point?

I already checked the Out-GridView, but this leaves Powershell to another window.

nobody
  • 19,814
  • 17
  • 56
  • 77
Kaffeepause
  • 208
  • 1
  • 3
  • 10
  • [CLRCLI](https://github.com/PhonicUK/CLRCLI) is a curses-style .Net project that's mentioned on [this blog post](http://tiberriver256.github.io/powershell/gui/PowerShell-Console-GUI/) about using it in Powershell. I was inspired by the links below to write [a simple ASCII menu](https://github.com/mossrich/PowershellRecipes/blob/master/AsciiMenu.ps1) that's surrounded by an ASCII box. – Rich Moss Apr 27 '19 at 01:06

1 Answers1

16

You need to use the System.Management.Automation.Host.ChoiceDescription .NET Framework class, which is designed for console menus.

For a very good example of use, see this Technet Blog article.

EDIT: I missed the fact that you wanted an arrow keys/enter key driven CLI menu.

I found a blog post from Jakob Bindslet that seems to be just what you wanted, although I confess I can't try it just yet, but I can't wait to!

EDIT: Another excellent option from Micahel Albert can be found on his blog; I found this option to be superior in that it does not clear the console window on arrow navigation.

RMD
  • 2,907
  • 30
  • 47
Graham Gold
  • 2,435
  • 2
  • 25
  • 34
  • 1
    The OP said he wants a menu where the user can make selections with arrow keys (presumably by highlighting the choice and selecting with [ENTER]), not console menus where you type your selection at a prompt below the menu. – Adi Inbar Jul 10 '13 at 22:28
  • Thanks @AdiInbar - I hadn't noticed that, must have skimmed over to quickly! – Graham Gold Jul 10 '13 at 23:35
  • 1
    Wow, thanks! Now I even know the term CLI Menu for what I am searching, haha! I'm gonna try it but I think this will fit my needs. – Kaffeepause Jul 11 '13 at 07:11
  • I modified the script By Micahel Albert, to use an [ordered] dictionary, which means the menu will thus appear in a more controllable/logical order. I also added wrap-around arrow key support. Works great. –  May 24 '17 at 13:14
  • I've modified the Jakob variant, which redrawing now only the changed lines and do not require console to be clear https://gist.github.com/hapylestat/b940d13b7d272fb6105a1146ddcd4e2a – Reishin Jul 21 '21 at 18:40