1

I have 16 different powershell scripts which install applications. How can I create a single batch file to ask for user input for selecting the powershell script to run?

Example:

I want to install only one application, so the batch script will ask for an input and I will type visualstudio. It will then run the appropriate powershell script to install Visual Studio.

SetLocal
    @echo on   
    :menu   

CHOICE /C VCNE /M "[V]isualStudio, [N]otePad, [c]hrome, [E]xit" 
    set answer=%CHOICE%
    if /i "%answer:~,1%" EQU "V" goto visualstudio
    if /i "%answer:~,1%" EQU "C" goto chrome
    if /i "%answer:~,1%" EQU "N" goto notepad
    if /i "%answer:~,1%" EQU "E" goto Exit


    :chrome
    C:
    cd\
    cd PowerShell
    powershell -file Chrome.ps1
    goto Menu

    :visualstudio
    C:
    cd\
    cd PowerShell
    powershell -file visualstudio.ps1
    goto Menu

    :notepad
    C:
    cd\
    cd PowerShell
    powershell -file visualstudio.ps1
    goto Menu

    :Exit
    exit
4
  • 1
    From the prompt, type choice /? which will give you all the flexibility you appear to require - and all single-key. Commented Jan 11, 2018 at 14:37
  • Thanks for the comment. tried the same but it goes to :chrome whatever the input is, Commented Jan 11, 2018 at 16:50
  • Try editing your modified code into your question so we can see and possibly fix what you are using. Commented Jan 11, 2018 at 16:58
  • It's because answer is empty, so you fall into the :chrome routine; where does %CHOICE% come from? have you read the help of the choice command that appears when typing choice /? into a command prompt window? Commented Jan 11, 2018 at 17:18

2 Answers 2

1
@echo off
CD /D "C:\Files" 
setlocal enabledelayedexpansion
powershell Set-ExecutionPolicy Unrestricted -force
powershell Set-ExecutionPolicy RemoteSigned
:List
CLS
set /A count=0
set choice=
for /R %%i in (*.ps1) do (
if not "%%~nxi" == "%~nx0" (
set /A count+=1
echo !count!: %%~nxi
set exe[!count!]=%%i
set choice=!choice!!count!
)
)
set /P EXENUM="Enter service number to INSTALL: "
set EXECUTABLE=!exe[%EXENUM%]!
echo running %EXECUTABLE%
pause
PowerShell -File "%EXECUTABLE%"
Pause
GoTo List
Sign up to request clarification or add additional context in comments.

2 Comments

This gives the exact result , no matter about the file numbers
this saves my entire time of execution and works like a charm, i have edited the code as per my requirements
1

The following example should create the menu at run time:

It works by searching the C:\Files directory for the .ps1 files and setting variables for use with the Set /P and PowerShell commands.

@Echo Off
CD /D "C:\Files" 2>Nul || Exit /B
If Not Exist *.ps1 Exit /B
SetLocal EnableDelayedExpansion

:Menu
ClS
For /F "Delims==" %%A In ('"Set MO[ 2>Nul"') Do Set "%%A="
Set "i=0"
For %%A In (*.ps1) Do (Set /A i+=1
    Set "MO[!i!]=%%A"
    Echo !i!. %%~nA)

:Retry
Set "MO[S]="
Set /P "MO[S]=Select an item number or press Enter to quit " || Exit /B
If Not Defined MO[%MO[S]%] GoTo Retry
Echo=PowerShell -ExecutionPolicy Bypass -File "!MO[%MO[S]%]!"
Pause
GoTo Menu

Notes:
The above example will just show you the intended command. If you are satisfied with the output, delete the penultimate line and remove Echo= from the line above it to actually run the selected powershell script.

It is obviously important that the names of your scripts are distinct enough to be clear to the end user what they are selecting.

You can modify:
Target directory, on line 2 (currently C:\Files)
Executable file extension, on lines 3 & 10 (currently .ps1)
Selection prompt message, on line 16 (currently Select an item number)
Command to run, on line 18 (currently PowerShell -ExecutionPolicy Bypass -File)

9 Comments

this output is absolutely fine , and testing the script, how can i have the explanation of the commands which you used , i understand only basics like echo, i am confused on %%~nA, is there any way to know about the mechanism ? thank you
You can enter each command used at the command prompt followed by /? and read the usage information. Other than that, I'm afraid you'll have to learn from trying things yourself and external resources, there are many sites dedicated to scripting in whichever language you prefer.
yes i can able to see the explanation for all the commands, but not exactly for the lines 7 and 9, and also i want to install some redistribute file before exit the code. i did tried added the command at last but it didnt worked, i want if user click on exit then it should this file and then exit, is there any possibility ?
For is one of the commands you'll take a while to get used to, it will however be used in almost every script you'll write. Take your time and don't worry, it'll probably take you some time to get it. If however you think you can bypass the leg work and expect me to teach you this scripting language, as part of my answer, then you're unfortunately going to be disappointed.
Thankyou so much, I have one issue if i have more than 8 files the script is automatically exiting and how can i insert the code for process the file after the enter pressed, Yes, you are right thanks for the valuable comments. i will start learn the scripting it is really interesting.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.