Microsoft announced that they will support SSH using PowerShell in Windows 10. Until now Microsoft has a good solution for this, there is a third party solutions called Posh-SSH. To use SSH in PowerShell you first have to install the Posh-SSH PowerShell Module from the PowerShell Gallery. Make sure you are running Windows 10 or you have the Windows Management Framework 5 installed.
If you want to use SSH with PowerShell 6, you read my blog here: Using SSH with PowerShell 6
You can now find the Posh-SSH Module and install it:
Install-Module Posh-SSH
You now have several PowerShell SSH cmdlets available:
Now you can create a new SSH Session using the following cmdlet (Password based authentication)
New-SSHSession -ComputerName "thomasmaurer.azure.cloudapp.net" -Credential (Get-Credential)
You can now simply run commands against this session or use SCP to copy files.
Invoke-SSHCommand -Index 0 -Command "uname"
I hope this helps you using SSH with PowerShell. If you have any questions, please write it in the comments. Also check out my other blogs and see how SSH is integrated in Windows 10.
Tags: Linux, Microsoft, Posh-SSH, PowerShell, PowerShell SSH, Remoting, SSH, SSH PowerShell, SSH PowerShell Module, Unix Last modified: April 4, 2019
Nice, very helpful. Now the questions is how can I user to connect to a windows server using ssh?
The real question is when will this support Kerberos authentication?
Does this also works with com ports?
I know I can use putty.
But I want to use this.
Because PowerShell.
To be honest, I haven’t tried it with com ports ;)
Can connect using buty bash.exe but not the module
New-SSHSession : A non-recoverable error occurred during a database lookup
At line:12 char:1
+ New-SSHSession -ComputerName $ipaddress -Credential $psCred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Renci.SshNet.SshClient:SshClient) [New-SSHSession], SocketException
+ FullyQualifiedErrorId : SSH.NewSshSession
what if my pc is only windows 7. who can i use SSH in powershell
@Chris
You can use this same guide but you just need to make sure you have PowerShell v5 installed on your Windows 7 PC
great. really nice.
how to exec. .bash scripts
shahrzad, did you install bash in windows yet? After you do that, you have full access to running any linux command.
https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/
question
exist a way to implement like
New-SSHSession -ComputerName “soaad1-q” -Credential ( user username password passwd )
This so called powershell must be the biggest joke in computer history. It has no, nada,zip, nil resemblance with a real shell.
Why? :)
Powershell is Awesome, If you don’t know how to use it. get-help
Linux subsystem on Windows so that you can run Linux commands and programs on Windows. It’s like building a mansion on a cliff. Eventually it’s going to fall because the cliff is unstable.
It is also not every command from Linux. The implementation is still limited for both commands and apps. In fact most apps will not run in this as there is still a substantial difference for how a true Linux OS performs compared to what this truly offers. Lots of hype here.
Even Jack Hammons at MS has written that this subsystem was geared towards bash and cmd line tools and that it doesn’t even support any desktops or apps. In fact, if an app does run, it’s largely because their required libraries were ported to Windows as well. In many cases, this has not happened. In the end, you are still stuck with the inherent limitations of Windows for this lipstick on a pig.
IF MS wants to do something good, create a gui for Linux or pull an apple, dump your core and port your ui to Linux. Lindows/Winux may actually be something that I’d consider from MS.
Back to the original item here of the article. You fail to mention the need for installation of and access for additional tools like nuget which is required with these steps. Nuget if you(the reader) don’t know is the package manager for MS’s developer tools and do you really want that on a potentially production server?! That to me just screams one more way for a Windows server to be hacked.
Yes PowerShell in general is pretty good if you are running Windows. But given the choice, and considering the impending explosion of PowerShell based malware, I’ll still stick to *nix and native shells there.
Cool. Thanks!
Why is it not install on my Windows 10 PC? It is a Windows 10 home 64bit.
Can this be used to build a tunnel instead of using
plink -R :127.0.0.1:80 @subdomain.domain.com -i private_ssh.ppk
I haven’t tried it with the PowerShell Module. But with WSL (Windows Subsystem for Linux) it works:
Windows 10: https://www.thomasmaurer.ch/2017/07/crazy-times-you-can-now-run-linux-on-windows-10-from-the-windows-store/
Windows Server: https://www.thomasmaurer.ch/2018/04/windows-subsystem-for-linux-on-windows-server/
For PowerShell version 2 compatibility, you can install the 1.9 or earlier versions of https://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library “SSH-Sessions” or “SSHSessions” (someone stole the name on powershellgallery). It’s here in the PowerShell gallery: https://www.powershellgallery.com/packages/SSHSessions/2.1.3 – it is also quite a bit less “bloat” than Posh-SSH. Two weeks ago I was at spot two on google for “powershell ssh module” with the PS gallery link, but now I’m relegated to just about nowhere, so I’ll post here for self promotion (rarely hurts). https://www.powershellgallery.com/packages/SSHSessions/
Or you could always turn to the light-weight alternative: SSH-Sessions/SSHSessions, written by yours truly.
Link is here: https://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library
And PowerShell Gallery link is here (the last PSv2-compatible version in old style is -RequiredVersion 1.9): https://www.powershellgallery.com/packages/SSHSessions/ – GitHub here: https://github.com/EliteLoser/SSHSessions – Thanks.
Can I use keys instead of using credentials?
Yes absolutely 👍
Some people will never learn. If you are doing admin tasks or data collection, most of the time this includes windows and Linux machines that you have to touch. That is the reason for this, not to jump off a cliff. Linux is not a replacement for Windows and Windows is not a replacement for Linux. They both have there place.
Powershell is a great tool and this is a great option. And yes, you can run this on Linux as well!
PS U:\> Invoke-SSHCommand -Index 0 -Command “config t”
Exception calling “EndExecute” with “1” argument(s): “An established connection was aborted by the server.”
At C:\Program Files\WindowsPowerShell\Modules\Posh-SSH\2.2\Posh-SSH.psm1:260 char:25
+ $Output = $_.cmd.EndExecute($_.Async)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SshConnectionException
I receive the above error message.
how do you pass sudo command creds to the linux machine?