How Can I Batch SSH into Multiple Devices and Run a Command?

March 21, 2026 5 answers
Asked by TechieNerd123
I'm looking for a simple PowerShell script that lets me SSH into a list of devices and execute a command automatically. The devices are all new and on a local network, and I don't care about the host key since it will change after I finish this initial task. I just want to accept the host key the first time, run a command, and then move on to the next device. I've got a CSV file with all the IPs listed. When I try to pipe 'y' to PLINK, it just hangs and does nothing. I need a straightforward solution to do this for 100 devices without manually connecting to each one. Is there an easy way to achieve this?

Answers

PowerShellWizard
Have you considered using Posh-SSH? It's a PowerShell module that simplifies SSH connections and could work better for your case. It allows you to handle the host keys more flexibly. Plus, instead of PLINK, it integrates more smoothly with PowerShell scripting.
Reply to this answer
CmdLineJunkie
I think you're overcomplicating it a bit. You donโ€™t actually need to log in first to accept the key; just connect. Try this in your PowerShell script: `echo y | plink -ssh user@device "exit"`. That should get you where you need to go without the hang-ups you're experiencing.
Reply to this answer
NinjaCoder88
Consider using threads or jobs for the connections if you're facing timeouts or slowdowns. Parallelizing your script can significantly speed up the process of handling multiple devices.
Reply to this answer
ScriptSorcerer
You can try using the `-o StrictHostKeyChecking=accept-new` option with SSH to automatically accept host keys. Alternatively, using `-o StrictHostKeyChecking=no` will also skip the prompt entirely, which should help you get through your list without any hang-ups.
Reply to this answer
OldSchoolTechie
Honestly, putty and plink are pretty dated. If you are set on scripting, why not use native SSH commands or tools instead? That could simplify your life a lot!
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions