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:
# promptsForInput.ps1
$someInput = Read-Host "Gimme some input"
# do something with $someInput
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:
# myScript.ps1
.\promptsForInput.ps1
At runtime, I get prompted to input something:
> .\myScript.ps1
Gimme some input:
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:
Summary: Learn how to prompt for input for your Windows PowerShell script. How can I use Windows PowerShell as an easy way to prompt a user for information for my script? Use Read-Host, for example: $a = Read-Host -Prompt "enter your input". The users input is stored in the $a variable.
Start a Powershell command-line. Request a user input using Powershell. Display the user input using Powershell. In our example, the user input was stored in a variable named NAME. Congratulations! You are able to request user input using Powershell.
Call the function of the Batch script. In our example, we created a function named MYPING. On the notepad application, create a Batch script named TEST. Here is the script result. Congratulations! You are able to prompt a user for input using a Batch script function.
Batch Script - Prompting for user input Start a command-line prompt. Request a user input using the command line. @echo off set /p MYNAME="Name: " Display the user input. echo Your name is: %MYNAME% In our example, the user input was stored in a variable named MYNAME. On the notepad application, create a Batch script named TEST.
Don't use $input
as the name of a variable as that's an automatic variable in PowerShell (see help about_Automatic_Variables
).
Determine whether the executable can accept command-line arguments instead of stopping or prompting for input. This is obviously preferred if it is available.
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
"test" | myexecutable.exe
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With