0

I have a batch file which prompts the user a few times. I am looking to automate that with powershell. Is there any way to do this? I would need something like this:

Start-Process $InstallDir\Install.bat "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"

Install.bat runs an installation and there are a total of 16 prompts. The third I would like the be a variable that I have in my powershell script already, but the others will be static. Also, at the end of the script, you need to press any key to continue.

Is there any way to do this?

9
  • 1
    The depends on what the batch file is actually doing. Simply passing a string argument is not likely to work, though. Commented Aug 22, 2016 at 14:03
  • 1
    Don't call the batch file at all. Put the commands from the batch file in a new PowerShell script and remove the prompts. Commented Aug 22, 2016 at 14:09
  • @Bill_Stewart this was an option, but the batch file is quite lengthy and it will take some time to do this. I was hoping for a faster solution, but if there is no other way, I can resort to this. Commented Aug 22, 2016 at 14:44
  • Sounds like it's something worth doing if you need the automation. Commented Aug 22, 2016 at 15:44
  • @Bill_Stewart it turns out I can't edit the batch files so I would need to find a way to pass these arguments. Commented Aug 22, 2016 at 17:09

2 Answers 2

1

Depending on your batch file and what commands actually do the prompt, you might use input redirection <. Put the prompts into a text file pine by line and redirect that into your batch file.

Supposing the batch file prompts.bat contains the following commands...:

@echo off
set /P VAR="Please enter some text: "
echo/
echo Thank you for entering "%VAR%"!
choice /M "Do you want to continue "
if not ErrorLevel 2 del "%TEMP%\*.*"
pause

...and the text file prompts.txt contains the following lines...:

hello world
Y
n
End

...the console output of the command line prompts.bat < prompts.txt would be:

Please enter some text:
Thank you for entering "hello world"!
Do you want to continue [Y,N]?Y
C:\Users\operator\AppData\Local\Temp\*.*, Are you sure (Y/N)?
C:\Users\operator\AppData\Local\Temp\*.*, Are you sure (Y/N)? n
Press any key to continue . . .

(The del command shows two prompts here as it receives the RETURN behind Y which is not consumed by choice; since an empty entry is not accepted, the prompt appears one more time.)

Sign up to request clarification or add additional context in comments.

3 Comments

While this works excellent in theory, it seems to be moving a little too fast for me. The batch file I am using copies a file after each prompt, which takes a while, so after the first file, it seems that all of the remaining prompts are left blank. Is there any way around that?
Hard to tell without seeing the batch file and the commands that are prompting... Let me suggest to ask a new question for that...
Ah, you did it; link for reference: Input redirects not working with batch file prompts
0

Read-Host will display a prompt for entry, assigning it to a variable means you can then use that entry later in the script.

As your example is non-specific the below will only give you an idea of what you need to do.

$InstallDir = "C:\folder"
$Version = Read-Host -Prompt "Enter Version Number"
Start-Process "$InstallDir\Install.bat" -ArgumentList "y,*,$Version,y,y,y,y,y,y,y,y,y,y,y,y,y"

1 Comment

Yes, but my question is how to pass these arguments into the batch file. You cant just use -ArgumentList to pass in answers to the prompts unfortunately, so I was looking for another way.

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.