Learning PowerShell – Lesson Nine
PowerShell Lesson Nine
By: Steven Aiello
Teach Your Self PowerShell –User Input & Making Menus with Switch
In the last few lessons we learned all about if, elseif, else, and loops, these are great to use if you need to make some choices however; choices are usually made based on user input. In this lesson we will introduce two new concepts. First is the command Read-Host which will allow us to get user input and the switch statement which will allow us to test user input against very specific parameters.
Its best practice in your scripts to only give users specific choices; you then look for those choices and produce an error or some condition for any other option but the ones they give you. Many times you will see a simple menu in a program that looks like the following:
It’s clear from the menu above will expect a choice 1 – 4 any other options besides 1 – 4 should tell the user they have done something incorrect. So how would we go about producing this menu and how do we get the input from the user?
We have all read seen that the way to print data to the shell is with the command Write-Host. If we want to read information from the shell we simple use the command Read-Host. Let’s see an example of what happens when we use the Read-Host command in our PowerShell script.
Notice the result? We have printed the menu using the Write-Host commands and when the script hit the line where the Read-Host command was it opened a popup menu for the user to input their selection. If you were to run your script in the basic PowerShell command window it would look like the image below
Whatever way you run your script the system will wait for you to provide input.
When you look at the actual code for this script you should notice something. We are asking the user for their input but, where are we saving it? Have we assigned the user input to any variable?
On further review of the image you should notice we haven’t assigned the user input to any variable. So what happens? The information simply goes away. The computer isn’t smart enough to know you want it to store that information any where. What we need to do is store the data that you give to the script for later use. We do this by assigning the value of the Read-Host command to a variable.
As you can tell from the code in the image I have created a variable names $SELECTION and have assigned to it the value that the user provides. You can see then on line 10 the option I selected in this case was option number 4. In this case 4 is a valid option but, what if I feel for tacos! What happens?
As you can see the script isn’t smart enough to know that I shouldn’t have entered a value of tacos. What we need to do is called “input sanitization of user data”. This is a very important concept when writing scripts and programs. You should never trust the data that your users give to you and you should only take action on data that you know is valid.
I’ll say it once again: Never trust user data!
So the question is now that we have this user input what do we do with it?
PowerShell Switch Statement
The PowerShell Switch statement is very much like if, elseif, else statements but, what if you had 10 if and elseif statements you needed to check? That would be a lot of code to write. That’s where Switch comes to the rescue. Let’s look at a complex if statement:
As you can see we have to test for all the possible numbers the user could enter in. This takes quite a few lines of code. The Switch statement is a simplified way to write this long nasty if & elseif statement.
Look at this code very carefully; can you understand what’s going on? Just like with the if statement you’re assigning the value to a variable however, instead of having to write if and elseif over and over you can tell the script in one location what variable to test. The limitation however of the Switch statement is that you can only test one variable.
Let’s step exactly how this works together.
Line 2: Sets the variable
Line 4: Is the beginning of the switch statement. What you’re stating is that you want the variable $MY_VARIABLE tested against every value listed.
Line 6: Tests against the number “1” and if the variable was equal to the number one the string “The number is 1.” would have been printed.
Line 11: Is where the condition actually evaluated to true. So when the condition was true the line was printed.
Line 14: default is the catch all, if none of the conditions matched the variable default catches any thing left at the end.
Let’s display an example where default catches the results of the switch statement.
Here we see we have set the variable $MY_VARIABLE to “Bob”. Clearly “Bob” will not evaluate to true on any of the lines 6 to 13. Because none of these lines evaluate to true the default condition at the bottom of the switch statement is executed. This is why switch statements are great for user menus because they will only accept valid user data and you can display an error if you don’t receive valid data.
So based on what we’ve learned so far let’s make a user menu.
In the above code we have simply taken a few items we’ve learned and put them together to make the start of a real script.
We print out a user menu using Write-Host
We read in the user input using Read-Host
We assign the Read-Host value to a variable
We use the variable in our switch statement to test what the user entered
If the user entered a correct value the script will tell the user what they wanted
If the user entered an incorrect value the script will tell them they can only pick an item on the menu
This is a great beginning to making some very powerful scripts! Writing scripts to take user input, test that user input, and then make choices on the user input is what lies at the heart of almost every script and program. Keep up the good work and you’ll be writing great scripts in no time!