1

The command below only works if I execute it from a Windows legacy cmd.exe prompt exactly like this:

powershell.exe -command "Write-Host -NoNewLine y ; sleep 2 ; Write-Host -NoNewLine y" | plink.exe ...

If I want to do the same thing from a PowerShell prompt, I have to do:

cmd /c powershell.exe -noprofile -command "Write-Host -NoNewLine y ; sleep 1 ; Write-Host -NoNewLine y" `| plink.exe ...

I'd like to know if there's a simpler & more streamlined way to do the same thing from a PowerShell prompt without breaking anything.

3
  • 2
    are you trying to echo 'y' character into the STDIN of plink.exe ?? Commented Apr 7, 2021 at 22:02
  • Without testing (or maybe I don't understand your goal), I guess you abuse host output. For stdin you should use System.Diagnostics.Process*. Commented Apr 7, 2021 at 22:05
  • Just to be clear, I'm not asking "how" to do it. I need a "y" keypress twice with the correct respective timing and the | character to be treated the same way it does under cmd.exe. I already posted the only two ways I could get the code working in the OP. successfully. I was just looking for a shorter way to do it than the "working" command-linea I posted in the OP. Commented Apr 10, 2021 at 2:35

1 Answer 1

2

Is your question "how do I pipe text to another program's standard input?" If that's your question, just quote the string and pipe to the program. Example:

PS C:\> "y" | plink ...

You would definitely not use Write-Host in PowerShell for this because output from Write-Host goes only to the host (screen) and cannot be redirected.

To pipe two y responses with a return in between, you would use this:

PS C:\> "y`ny" | plink ...

You say that Write-Host "works" in your scenario. This would mean the y keystroke actually isn't being passed (Write-Host does not produce redirectable output, as noted) and your command is only "working" completely by accident.

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

2 Comments

That's correct! Quoting as such defaults to Write-Output which keeps the text as an object.
I'm not asking "how" to do it. The methods mentioned above will not work in my case; as, I need "y" keypress twice with the correct respective timing. I already posted the only way I could get the code working in the OP. It doesn't affect the results either way if I use Write-Output or Write-Host for this case in my example.. as they both work. Anyway, I was just looking for a shorter way to do it than the "working" command-line I posted in the OP.

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.