3

If I want to overwrite existing SSH key file without typing y, then I would do this in bash:

echo "y" | ssh-keygen -f /home/wakatana/id_rsa -N ""

How can same be achieved using PowerShell? I've tried following:

Write-Host "y" | ssh-keygen.exe -f C:\Users\wakatana\Desktop\id_rsa -N """"

It won't show me Overwrite (y/n)? dialog, but I does not rewrite existing key. What is the problem?

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
Wakan Tanka
  • 6,662
  • 12
  • 58
  • 109

2 Answers2

2

Just delete the file if it already exists, before calling ssh-keygen.

Blindly sending "yes" to an application, without knowing, what does it ask in the first place, is quite dangerous.

Martin Prikryl
  • 167,268
  • 50
  • 405
  • 846
0

Try Write-Output instead of Write-Host:

Write-Output "y" | ssh-keygen.exe -f .\test -N "blahblah"

enter image description here

Glenn
  • 1,469
  • 13
  • 20
  • 1
    `Write-Output` is superfluous here. – Bill_Stewart Dec 30 '19 at 18:52
  • Good point by @Bill_Stewart . This should work as well: `"y" | ssh-keygen.exe -f .\test -N "blahblah"` – Glenn Dec 30 '19 at 19:43
  • thank you very much. This worked. Can you please explain why `Write-Host` is not working while `Write-Output` is? – Wakan Tanka Jan 03 '20 at 12:23
  • @WakanTanka [here](https://stackoverflow.com/questions/8755497/whats-the-difference-between-write-host-write-output-or-consolewrite) is a good explanation. In my own words, `Write-Host` send output to the "host", which can vary, but is usually the command console. `Write-Output` sends the output through the pipeline and to STDOUT, which is then interpreted as the STDIN for the next native command in the pipeline. – Glenn Jan 03 '20 at 13:10