Complete Guide to Using SSH from PowerShell: How to Connect, Manage, and Automate Remote Sessions

  • PowerShell allows native SSH connections in Windows, Linux and macOS to securely manage remote systems.
  • OpenSSH is easy to install and configure, allowing for integrated password or public key authentication.
  • PowerShell cmdlets support remote SSH sessions, command execution, and file transfer without relying on external software.

How to use SSH from PowerShell

Nowadays, remote system administration is a must for any system administrator, developer or computer enthusiast working with different servers or PCs, whether they are Windows, Linux or even macOS. The appearance of PowerShell with native support for SSH has been a real catalyst. on how to connect and manage computers securely and effectively. If you're one of those who still think you need external software or that the process is complicated, keep reading: this article will clear up all your doubts and serve as a complete guide to using SSH from PowerShell, covering installations, commands, advanced steps, and practical tips for different environments.

Being able to connect via SSH from PowerShell not only allows you to manage servers without leaving your chair, but also saves you from having to install third-party applications, providing a native, secure, and cross-platform experience. The integration of SSH into PowerShell has greatly simplified automation and administration tasks, both in infrastructures Windows as in mixed environments, where systems from different manufacturers coexist.

What is SSH and why use it with PowerShell?

SSH (Secure Shell) is a protocol that allows you to connect to other computers over the network in an encrypted and secure manner. Replaces older protocols like Telnet, ensuring that any data sent (credentials or commands) travels protected against possible interception. It has been the standard for secure remote access for years, especially on Linux servers, but its use in Windows has been growing thanks to the integration of OpenSSH in the system itself and in PowerShell.

With PowerShell, SSH commands can be executed from the console Windows or scripts, manage files, automate tasks and connect to both servers Windows such as Linux or network devices – all from a single tool. This makes it a very flexible and powerful solution.

Advantages of using SSH from PowerShell

  • Multi platform: Manage systems Windows, Linux and macOS without switching applications.
  • Simple automation: Integrate SSH commands into your PowerShell scripts for recurring tasks.
  • No additional software: Forget about installing external clients like PuTTY, as the functionality comes standard in Windows 10, 11 and Server 2019 or higher.
  • Safety: SSH encrypts all communication and supports password or key authentication.

Requirements for using SSH in PowerShell

Before you begin, make sure you meet the following basic requirements:

  • PowerShell version 5.1 or higher (PowerShell 6 or 7 recommended for SSH compatibility and advanced features).
  • SSH client installed. In Windows 10 since version 1809 and Windows Server 2019, OpenSSH comes preinstalled. For earlier versions, you can install it manually from the optional features section. Windows.
  • SSH server activeThe computer you want to connect to must have a running SSH server, whether it's a Linux server, another Windows, a macOS, or some compatible network device.
  • Appropriate permissionsYou must have valid credentials on the remote computer (username and password or an SSH key) and preferably be an administrator if you're installing or configuring services.
  • Network access and the 22 port open in the firewall to allow SSH connections.

Installing and configuring OpenSSH in Windows

OpenSSH integration in Windows It's one of the biggest advances in remote administration with PowerShell. Here are the most important steps to properly install and configure it:

1. Check for the presence of OpenSSH

Open PowerShell as administrator and run:

get-WindowsCapability-Online | Where-Object Name -like 'OpenSSH*'

If you see the status "Installed" for OpenSSH.Client or OpenSSH.Server, you already have it available. If it says "NotPresent," you'll need to install it.

2. Install the OpenSSH client and/or server

Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0 Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0

The first allows you to connect to other computers, the second allows others to connect to your PC.

3. Start and configure the OpenSSH service

Start-Service sshd Set-Service -Name sshd -StartupType 'Automatic'

This will enable the service and set it to start automatically with Windows.

4. Firewall Configuration

Make sure your firewall is Windows Allows SSH connections on port 22. When you install OpenSSH, the rule is usually created automatically, but to verify this, you can use:

Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP"

If it doesn't exist, you can easily add it:

New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22

Configure SSH on Linux, macOS and Windows for remote PowerShell sessions

SSH in PowerShell isn't just for connecting to Linux systems. Starting with PowerShell 6, it can be used to run remote PowerShell sessions (PSRemoting) using SSH as the transport, enabling truly cross-platform sessions.

Configuration in Windows

  1. Install the latest version of PowerShell. You can download it from GitHub.
  2. Edit the sshd_config file (available in $Env:ProgramData\ssh):
    • Enable password authentication:
      PasswordAuthentication yes
    • Add the subsystem for PowerShell:
      Subsystem powershell C:/progra~1/powershell/7/pwsh.exe -sshs

      You can create a symbolic link to facilitate the path of the executable:

      New-Item -ItemType SymbolicLink -Path 'C:\ProgramData\ssh\' -Name 'pwsh.exe' -Value (Get-Command pwsh.exe).Source
    • If you want to use key authentication:
      Pubkey Authentication yes
  3. Restart the sshd service:
    Restart-Service sshd
  4. Make sure the OpenSSH path is in the PATH variable.

Configuration in Linux

  1. Install PowerShell from the official repository ().
  2. Install the SSH server:
    sudo apt install openssh-client sudo apt install openssh-server
  3. Edit the /etc/ssh/sshd_config file:
    • Secure the line:
      PasswordAuthentication yes
    • Add the PowerShell subsystem:
      Subsystem powershell /usr/bin/pwsh -sshs -NoLogo
    • For public key authentication:
      Pubkey Authentication yes
  4. Restart the SSH service:
    sudo systemctl restart sshd.service

Settings on macOS

  1. Install the latest version of PowerShell.
  2. Activate remote access (Remote Login) in system preferences, 'Sharing' section.
  3. Edit /private/etc/ssh/sshd_config and adds:
    • PasswordAuthentication yes
    • Subsystem powershell /usr/local/bin/pwsh -sshs -NoLogo
    • Pubkey Authentication yes
  4. Restart the SSH service:
    sudo launchctl stop com.openssh.sshd sudo launchctl start com.openssh.sshd

Remote connection and management with PowerShell and SSH

There are two main ways to use SSH from PowerShell:

  • Run command ssh directly from the console, just like on Linux or macOS systems.
  • Use PowerShell-specific cmdlets for remote sessions: New-PSSession, Enter-PSSession, Invoke-Command, which support an SSH connection as a transport, ideal for running commands or scripts on other computers.

Basic connection using the ssh command

ssh user@IP_or_host_name

You will be prompted for your password, or if you have public key authentication set up, it will be used directly.

You can use all the usual Linux commands, and also manipulate files, restart services, etc. To end the session use exit.

PowerShell Cmdlets with SSH

Starting with PowerShell 6, you can create remote sessions using SSH. Examples:

$session = New-PSSession -HostName RemoteServer -UserName yourUser Enter-PSSession $session

To run a single remote command:

Invoke-Command -Session $session -ScriptBlock { Get-Process }

To release the session:

Remove-PSSession $session

Practical examples: from Windows to Linux and vice versa

Imagine you have a Windows 11 and you want to access your Ubuntu server. The steps would be:

  1. Open PowerShell in Windows.
  2. Email:
    ssh yourUser@Ubuntu_IP
  3. Enter the password when prompted.
  4. You can now run commands on your remote server.

The process is identical for connections between Linux and Windows, just changing the user and the destination computer.

File Transfer with PowerShell and SSH

Another very practical utility is the use of scp (secure copy), which is also available in Windows with OpenSSH and allows you to transfer files between local and remote computers.

scp file.txt user@IP:/path/destination/

Or the other way around, download from the remote server:

scp user@IP:/path/file.txt ./

Useful PowerShell-Linux Commands and Equivalents

  • ls / dir: Displays the contents of a directory
  • CD: Change directory on the remote server
  • rm / Remove-Item: Delete files or folders
  • mv / Move-Item: Move or rename files
  • touch / New-Item: Create files
  • useradd: Add users (on Linux)
  • scp: Copy files between computers

SSH key management for advanced authentication

Using a password is fine to start with, but the safest and most practical is public key based authentication:

    1. Generate an SSH key:
      ssh-keygen
    2. Add the public key to the remote server: Copy the contents of id_rsa.pub (Linux) or the generated file in Windows to ~/.ssh/authorized_keys on the destination server.
    3. Optionally, use ssh-agent to manage multiple keys:
ssh-add private_key_path

This way, you'll only have to enter the key the first time, or not at all, if the ssh-agent is properly configured.

Limitations and Important Considerations

  • The sudo command It's not directly available in remote PowerShell-SSH sessions on Linux. To run privileged commands, you'll need to find alternatives or specific methods.
  • PowerShell profiles ($PROFILE) are not loaded automatically during SSH sessions. If you need to use one, you'll need to load it manually after starting the remote session.
  • Cross-platform limitations: Some advanced features (such as endpoint management or JEA) are only supported by WinRM, although further support under SSH is expected in the future.
  • Second hop of remote sessions: Prior to PowerShell 7.1, it was not possible to create a second remote session from an already active one using SSH, but starting with that version, it is now possible, albeit with certain restrictions.

Specialized use: switches, network devices, and REST APIs

PowerShell can connect via SSH to network devices such as switches to execute commands, although it depends on the firmware's capabilities and SSH protocol support. For more advanced tasks, some hardware manufacturers offer REST APIs that can also be managed from PowerShell.

In many cases, The direct method with SSH is the most universal option, simple and compatible for basic operations on the remote computer.

Alternatives to OpenSSH? PuTTY and other clients

Although OpenSSH now comes integrated into Windows, tools like PuTTY are still valid options, especially if you need a graphical interface or to manage saved connections with different configurations and keys. However, direct integration with PowerShell and scripts makes OpenSSH the preferred option for most users and automation tasks.

What if I work with GitHub or similar platforms?

Setting up SSH keys in PowerShell is essential if you use platforms like GitHubThe procedure consists of generating the key, adding it to the agent, and copying the public key into your GitHub profile settings. Once this is done, you can work with your repositories securely without entering the password every time.

Mastering SSH in PowerShell opens up a world of possibilities for managing and automating tasks, whether you manage servers Windows, Linux or network devicesThanks to native OpenSSH integration and the power of PowerShell cmdlets, controlling remote systems is a matter of minutes. From initial installation and configuration to file transfers and executing complex commands, any user can turn their PC into a true, secure, and versatile cross-platform operations center.

WSL 2 (Windows Subsystem for Linux 2): All about the Subsystem for Linux Windows for Linux, advantages and how to take advantage of it

Leave a comment