In Windows I have console programs that run in the background with the console hidden. Is there anyway to direct input to the programs console? I want to be able to do something like:
echo Y| *the_running_process_here*
to send Y to the process' stdin.
On a command line, redirection is the process of using the input/output of a file or command to use it as an input for another file. It is similar but different from pipes, as it allows reading/writing from files instead of only commands. Redirection can be done by using the operators > and >> .
Used without any command line parameters, the yes command behaves as though you were typing “y” and hitting Enter, over and over (and over and over) again. Very quickly. And it will carry on doing so until you press Ctrl+C to interrupt it.
As far as I know this is not possible by basic cmd
commands. PowerShell is more powerful though and can use the .NET framework from windows.
I found this PowerShell script which claims to pass text to an already opened cmd
:
$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "cmd.exe"; #process file
$psi.UseShellExecute = $false; #start the process from it's own executable file
$psi.RedirectStandardInput = $true; #enable the process to read from standard input
$p = [System.Diagnostics.Process]::Start($psi);
Start-Sleep -s 2 #wait 2 seconds so that the process can be up and running
$p.StandardInput.WriteLine("dir"); #StandardInput property of the Process is a .NET StreamWriter object
Source: https://stackoverflow.com/a/16100200/10588376
You would have to safe this script with an .ps1
ending and run it.
A workaround to use this as a cmd
command would be to make it accept arguments, so that you can run it with yourScript.ps1 procced_pid arguments
for example.
The standard windows cmd
is just too limited to fullfill such a task by its own.
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