I got a question from Justin this week:
Have a few scripts that I use for regular admin work. I want to combine them with a simple menu. I was wondering if there was an easy way to make a menu in a batch file.”
Yes there is Justin, and it is actually quite simple.
The SET command is perfect for this. When you call SET /P from the command line it will take a line of input and set it to a variable.
So if you had three options in your batch file like this:
echo Press 1 for Option1
echo Press 2 for Option2
echo Press 3 for Option3
echo Press 4 to Exit
You could put the set command right after it:
set /P OPT=Please make a selection and press enter
This would print the text “Please make a selection..” and then store the users input after they press enter – into the %OPT% variable.
Once that happens we can use a few if statements to conditionally execute.
Here is it all together:
ECHO 1 - Option 1
ECHO 2 - Option 2
ECHO 3 - Option 4
ECHO 4 - Exit
SET /P OPT=Please make a selection, and press enter:
if %OPT%==1 GOTO OPTION1
if %OPT%==2 GOTO OPTION2
if %OPT%==3 GOTO OPTION3
ECHO Executing option1!
ECHO Executing option2!
ECHO Executing option3!
Save the file as menu.bat and run it
That is all there is to it.
Just change the echo statements to the batch commands you want to execute for each option, and you have an easy to use menu from the command line.
One more thing…Subscribe to my newsletter and get 11 free network administrator tools, plus a 30 page user guide so you can get the most out of them. Click Here to get your free tools