, , , ,

Using SSH with PowerShell

1–2 minutes
25 comments on Using SSH with PowerShell
PowerShell SSH Session

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:

PowerShell SSH Installation


Install-Module Posh-SSH

You now have several PowerShell SSH cmdlets available:

PowerShell SSH cmdlets

Now you can create a new SSH Session using the following cmdlet (Password based authentication)

PowerShell SSH Session


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.

PowerShell SSH Commands


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.

25 responses

  1. Elio Avatar

    Nice, very helpful. Now the questions is how can I user to connect to a windows server using ssh?

  2. Blake Avatar

    The real question is when will this support Kerberos authentication?

  3. Remco Avatar

    Does this also works with com ports?
    I know I can use putty.
    But I want to use this.
    Because PowerShell.

    1. Thomas Maurer Avatar

      To be honest, I haven’t tried it with com ports ;)

  4. mert Avatar

    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

  5. Chris Avatar

    what if my pc is only windows 7. who can i use SSH in powershell

  6. JayP Avatar

    @Chris

    You can use this same guide but you just need to make sure you have PowerShell v5 installed on your Windows 7 PC

  7. shahrzad Avatar
    shahrzad

    great. really nice.
    how to exec. .bash scripts

  8. Jonathan White Avatar
    Jonathan White

    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/

  9. Virgil Glavan Avatar
    Virgil Glavan

    question
    exist a way to implement like
    New-SSHSession -ComputerName “soaad1-q” -Credential ( user username password passwd )

  10. Thommy M. Malmström Avatar
    Thommy M. Malmström

    This so called powershell must be the biggest joke in computer history. It has no, nada,zip, nil resemblance with a real shell.

  11. Remco de Rooij Avatar
    Remco de Rooij

    Powershell is Awesome, If you don’t know how to use it. get-help

  12. Steve Avatar

    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.

  13. Tyler Avatar

    Cool. Thanks!

  14. Abbas Cloud Avatar

    Why is it not install on my Windows 10 PC? It is a Windows 10 home 64bit.

  15. guest Avatar

    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

  16. Joakim Svendsen Avatar

    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/

  17. Joakim Borger Svendsen Avatar

    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.

  18. Sagar Pardeshi Avatar
    Sagar Pardeshi

    Can I use keys instead of using credentials?

    1. Thomas Maurer Avatar

      Yes absolutely 👍

  19. Mark Schaefer Avatar
    Mark Schaefer

    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!

  20. AMIT JOGI Avatar
    AMIT JOGI

    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.

  21. robert Avatar

    how do you pass sudo command creds to the linux machine?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.