This post is a short note on how to use SSH with Windows Powershell. I will quickly describe three ways: OpenSSH, Posh-SSH and Putty. I found a few blog posts about how to use SSH with Powershell and most of them are referring Posh-SSH. Posh-SSH is nice, but I think OpenSSH is much easier to use because it works the same way as the ssh command in linux.
Open SSH for Powershell
If chocolatey is not yet installed, you must at first install it. Run the Powershell as administrator and execute:
Set-ExecutionPolicy Unrestricted iwr https://chocolatey.org/install.ps1 -UseBasicParsing | iex
If chocolatey is already installed, run the Powershell as administrator and execute the following command to install OpenSSH, to reload the environment variables and to connect to a client:
choco install openssh # installs open ssh refreshenv # reloads the environment variables ssh remoteClient -i "MyKeyPair.pem" # connects to remoteClient via ssh
Posh-SSH Powershell module
Posh-SSH is a module for Powershell that can also be used for SSH connections. Install it via the Package manager (as administrator) and then you can establish a connection. All that can be done with the following commands:
Install-Module Posh-SSH # installs the posh-ssh module Get-Command -Module Posh-SSH # shows all posh-ssh commandlets New-SSHSession myclient -KeyFile "c:\data\MyKeyPair.pem" # connect to my client with the give keyfile Invoke-SSHCommandStream "ifconfig" -SessionId 0 # send ifconfig to the ssh session with id 0 Invoke-SSHCommand -SessionId 0 -Command "ifconfig" # send ifconfig to the ssh session with id 0 Invoke-SSHCommand -SessionId 0 -Command "logout" # send logout to the ssh session with id 0 Remove-SSHSession 0 # removes and closes the ssh session
Putty and the Powershell
The third way I want to show here is to use Putty inside of the Powershell. Install Putty via chocolatey and then you can use plink to run Putty commands (if you don’t want to use the Putty GUI). The installation plus establishing a connection can be done via:
choco install putty plink -ssh myclient
Additional information
The Universal Openssh Installer: https://github.com/DarwinJS/ChocoPackages/tree/master/openssh
Posh-SSH: https://github.com/darkoperator/Posh-SSH
Chocolatey – Package management for Windows: https://arminreiter.com/2017/01/chocolatey-package-management-windows/
2 Responses
[…] SSH for Windows Powershell […]
That was really usefull.
Thank you, Herr Reiter.
[]s
LKTT