r/PowerShell • • Jul 20 '19

How to build a PowerShell menu

https://adamtheautomator.com/build-powershell-menu/
123 Upvotes

29 comments sorted by

8

u/Ghlave Jul 20 '19

Nice article but it is very difficult to read. The formatting you have in place makes the snippet below have a rather large horizontal scroll bar, even on a 1920x1080 maximized web browser.

function New-Menu {
[CmdletBinding()]
param(
    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$Title,

    [Parameter(Mandatory)]
    [ValidateNotNullOrEmpty()]
    [string]$Question
)

$red = New-Object System.Management.Automation.Host.ChoiceDescription '&Red', 'Favorite color: Red'
$blue = New-Object System.Management.Automation.Host.ChoiceDescription '&Blue', 'Favorite color: Blue'
$yellow = New-Object System.Management.Automation.Host.ChoiceDescription '&Yellow', 'Favorite color: Yellow'

$options = [System.Management.Automation.Host.ChoiceDescription[]]($red, $blue, $yellow)

$result = $host.ui.PromptForChoice($Title, $Question, $options, 0)

switch ($result) {
    0 { 'Your favorite color is Red' }
    1 { 'Your favorite color is Blue' }
    2 { 'Your favorite color is Yellow' }
}

}

5

u/adbertram Jul 21 '19

Thanks for the feedback. Code formatting is always tough to get right.

6

u/_stib_ Jul 21 '19

It can't be read properly at all on my phone, the code boxes don't scroll, so the head and tail at chopped off (boost on Android)

2

u/adbertram Jul 21 '19

It’s really hard to get it to look properly on all browsers. I don’t have an Android to test with but I’ll see what I can do.

Can you send me a screenshot?

2

u/fyritka Jul 21 '19

You can use the developer inspector in browsers to see how things will look on different size screens. :D It's pretty nifty! I am on my phone so I can't send a screenshot of where the Layout menu is at, but I can check this l8r if you don't find it.

Edit: https://developer.mozilla.org/en-US/docs/Tools/Responsive_Design_Mode for example

2

u/adbertram Jul 21 '19

I have a web person that helps me out but he can't replicate it. :/

1

u/alistek Jul 22 '19

/u/stib I pushed some updates for this, let me know on a hard refresh if it looks better. I lowered the font size a bit too for mobile as well. Thanks! CC: /u/adbertram

0

u/[deleted] Jul 21 '19

[deleted]

1

u/adbertram Jul 21 '19

I know you can do that with C#. Can you do that with PowerShell?

3

u/SevaraB Jul 21 '19

Just took a stab at refactoring and tested this in ISE:

using namespace System.Management.Automation.Host

$red = [ChoiceDescription]::new('&Red', 'Favorite color: Red')
$blue = [ChoiceDescription]::new('&Blue', 'Favorite color: Blue')
$yellow = [ChoiceDescription]::new('&Yellow', 'Favorite color: Yellow')

$options = [ChoiceDescription[]]($red, $blue, $yellow)

$title= 'Favorite color'
$message = 'What is your favorite color?'
$result = $host.UI.PromptForChoice($title, $message, $options, 0)

It probably is the same as C#, since it's not so much Powershell or C# as it is .NET, which is what both are built on.

2

u/adbertram Jul 21 '19

I've been using PowerShell since Monad and had no idea you could use using. Thanks for the tip!

1

u/get-postanote Jul 21 '19

It's been in the PowewrShell documentation for a long while now.

1

u/adbertram Jul 21 '19

I’m not too keen on scouring the docs nightly.

1

u/get-postanote Jul 22 '19

;-}

Yeppers, I get that. That TL;DR or T2;LTU stuff can be a regular hurdle for us all.

1

u/adbertram Jul 21 '19

Got it updated and gave you credit.

0

u/[deleted] Jul 21 '19

[deleted]

1

u/adbertram Jul 21 '19

Oh shoot. I’ll update it.

1

u/SevaraB Jul 21 '19

Yeah, /u/ttwinlakes deserves the credit. But hey, glad the demo helped!

7

u/JeremyLC Jul 21 '19

Your site layout is decidedly mobile hostile.

http://imgur.com/a/OXfOF04

2

u/adbertram Jul 21 '19

I don't see this on iOS. Can you tell me what Android version you're using and browser?

1

u/JeremyLC Jul 21 '19

Android 9 on a Pixel 3 XL

1

u/adbertram Jul 21 '19

Hmm...that shouldn't be. Let me look into that.

1

u/JeremyLC Jul 21 '19

Android 9 on a Pixel 3 XL

1

u/adbertram Jul 21 '19

Thank you! Is the entire site just black or portions of it?

2

u/SpacezCowboy Jul 20 '19 edited Jul 20 '19

If you use a do-until loop the menu can be returned after each option. Useful for a menu that can allow for running multiple switches in a serial order. Use case would be to execute a process of multiple steps where the next step can't be run until the previous steps have had time to complete. I've used this to make an easily selectable set of options, and walk the less skilled through handling the task themselves.

3

u/tkecherson Jul 20 '19

Mine uses the Do .. Until to force a user to choose one of the given options, if the returned value isn't a choice it loops back until the user does it right.

2

u/adbertram Jul 21 '19

I’ve done that as well. I should update it to include that at some point.

1

u/hngovr Jul 21 '19

How would i change the text color of the menu?

3

u/tkecherson Jul 20 '19

Interesting. I could have used this a week and a half ago when my boss told me to make my AD management module into a menu-based script, but I managed to get it working. I'll have to go through this on a PC later and see if I can improve what I have. Thanks!