Menu function run commands

Hey all.

I need a script with a menu. The purpose with the menu is that, if i ex. press 1 it should run a command, in this case install a printer, but i cant quite find out where to start.

Im not that hardcore to powershell, but i have to learn somehow :slight_smile:

I googled it for you …

https://www.google.com/search?q=powershell+text+menu

This was the first hit:

I have googled that too :slight_smile: but the problem is, how do i make a selection that runs a command when i press 1. ex. add-printer “and the command here” i hope this make sense

Actually it does not, sorry.

The easiest and most promissing way of asking questions about code is to share the code you have problems with. :wink:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

Well i can try to show with a small script where you can select a color.

$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)
$title = 'Favorite color'
$message = 'What is your favorite color?'
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

If the red color is ex. printer 1 then when you press red in the popup window, then it should run this command:

add-printer -connectionname "here is where the printer is located"

if i should make it so i can select the network printer and install it, from ex. a .txt file where the printer installation has the code to install or just select the printer from the list.

Does this make sence?

Please go back, edit your post again and fix the formatting of the code. Use the description I posted in my last post.

Thanks in advance.

Sorry m8, there you go :slight_smile:

You should have read the rest of the blog post about how to build a PowerShell menu as well … :point_up_2:t3: :point_up_2:t3:

Getting the user input is just the first step. Now you have to evaluate it either with a simple if condition if it’s just a “yes” or “no” choice or with a switch statement if there are more options available. :man_shrugging:t3:

Sorry but im not that hardcore to scripting. Im in the beginning learning stage :slight_smile: