How to make a Powershell script answer a prompt for user input?

Question:

I have a Powershell script that needs to execute a command-line executable that prompts for user input (for which there is no command-line parameter). Something like this:

The actual executable is a compiled binary which I cannot modify, but this example script do for the purposes of this question. My Powershell script calls the executable:

At runtime, I get prompted to input something:

I need to be able to fill in that input request without user intervention. When promptsForInput.ps1 prompts for user input, I need myScript.ps1 to enter the required value and proceed with script execution.

Similar questions on SO either have answers specific to the particular exe, or are otherwise unhelpful:

Answer:

  1. Don’t use $input as the name of a variable as that’s an automatic variable in PowerShell (see help about_Automatic_Variables).
  2. Determine whether the executable can accept command-line arguments instead of stopping or prompting for input. This is obviously preferred if it is available.
  3. Whether you can tell PowerShell to automatically send input to an arbitrary executable depends upon the design of that executable. If the executable allows you to send standard input to it, you can run something like

    In this example, PowerShell will provide the string test and a return as standard input to the executable. If the executable accepts standard input, then this will work.

    If the executable does not use standard input, then you will have to do something different like send keystrokes to the window. I consider sending keystrokes a last resort and a brittle solution that is not automation-friendly.

Source:

How to make a Powershell script answer a prompt for user input? by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply