PowerShell Inside All articles
Cross-Platform Automation

PowerShell and SSH: A Practical Guide to Keys, Remoting, and Automation

PowerShell Inside
PowerShell and SSH: A Practical Guide to Keys, Remoting, and Automation

SSH is one of those tools that feels almost too simple until you try to wire it into a Windows workflow. You type ssh user@host in PowerShell, it works, and you move on. Then you need keys, a jump host, an automated script, or PowerShell remoting to a Linux box, and suddenly you are reading man pages at 11 p.m. This guide is the middle ground: the commands and configuration that make SSH from PowerShell predictable in 2026, without turning into a 40-page reference.

First, know which ssh you are running

On Windows, ssh is usually the OpenSSH client that ships with Windows 10 and Windows 11. Check it with:

Get-Command ssh
ssh -V

If that returns nothing, install the OpenSSH Client optional feature from Settings > Apps > Optional Features, or from an elevated PowerShell session:

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

On macOS and Linux, ssh comes from the system package manager or is already present. PowerShell 7 runs the same native ssh binary as your shell, so the syntax is consistent. That is the good news. The bad news is that Windows paths, key permissions, and agent behavior still have their own personality.

Use keys, not passwords

Passwords are fine for a one-off lab VM. They are a bad fit for automation. Generate an Ed25519 key pair:

ssh-keygen -t ed25519 -C 'you@workstation'

Press Enter to accept the default path, or choose a project-specific key. Then copy the public key to the remote host. On Windows, this one-liner works in PowerShell:

type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@host 'mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys'

If you are automating against many hosts, do not paste that command into fifty scripts. Use a configuration file.

The config file is the real productivity feature

Create or edit $env:USERPROFILE\.ssh\config on Windows, or ~/.ssh/config on macOS and Linux. A practical entry looks like this:

Host web-prod
    HostName 203.0.113.10
    User deploy
    IdentityFile ~/.ssh/id_ed25519_web
    Port 2222

Host db-prod
    HostName 203.0.113.20
    User dba
    ProxyJump bastion

Now ssh web-prod does the work of a long command line. It also keeps scripts readable: ssh db-prod is easier to audit than a command with an IP, username, port, and key path. Verify what SSH actually resolved with:

ssh -G web-prod

That prints the effective configuration, including defaults you did not write. It is the fastest way to catch a typo in IdentityFile or a Host block that never matches.

Get the SSH agent working on Windows

The OpenSSH Authentication Agent service is built into Windows but not always running. Start it and set it to automatic:

Set-Service ssh-agent -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519

After that, ssh-add -l should list your key. If it does not, you are probably running the command in a different user context or the service is stopped. This matters for scheduled tasks and CI agents, where an interactive key prompt will simply hang.

PowerShell Remoting over SSH

This is where PowerShell and SSH stop being just neighbors. PowerShell 7 supports remoting over SSH, which lets you run PowerShell commands on a Linux host from a Windows workstation without WinRM. On the remote Linux machine, install PowerShell 7 and make sure sshd is running. Then edit /etc/ssh/sshd_config and add:

Subsystem powershell /usr/bin/pwsh -sshs -NoLogo

Restart sshd, then test from Windows:

New-PSSession -HostName user@host -SSHTransport

If that succeeds, you can use Enter-PSSession for an interactive session or Invoke-Command for one-off automation:

Invoke-Command -HostName user@host -SSHTransport -ScriptBlock {
    Get-Process | Sort-Object CPU -Descending | Select-Object -First 5
}

A few practical notes. Use -HostName, not -ComputerName, when you want SSH transport. Authentication is handled by SSH, including keys and the agent, so set that up first. And if you are connecting to a Windows host over SSH, the remote side needs OpenSSH Server and a PowerShell subsystem configured as well. The transport is cross-platform; the setup on each end still matters.

Make SSH behave in scripts

Interactive SSH is forgiving. Scripts are not. For automation, force non-interactive behavior and fail loudly:

ssh -o BatchMode=yes -o StrictHostKeyChecking=accept-new web-prod 'hostname'
if ($LASTEXITCODE -ne 0) {
    throw 'SSH command failed'
}

BatchMode=yes prevents password prompts from hanging a scheduled task. StrictHostKeyChecking=accept-new accepts a new host key on first contact but still refuses a changed key later, which is a reasonable default for controlled environments. For file transfer, scp and sftp use the same config file and keys:

scp .\deploy.zip web-prod:/tmp/deploy.zip

Do not parse the banner text from SSH. Use exit codes, or have the remote command emit structured output such as JSON. That is the same rule you would apply to any other command-line tool in PowerShell.

A quick troubleshooting checklist

When SSH from PowerShell misbehaves, check these in order: Get-Command ssh to confirm which binary runs; ssh -V for the version; ssh -vvv user@host for the verbose handshake; file permissions on the private key and .ssh directory; the known_hosts entry if the host was rebuilt; and the ssh-agent service if keys are not being offered. On Windows, also confirm that the optional OpenSSH Client feature is installed and that your terminal is not running as a different user than the one where you created the key.

SSH does not need to be mysterious. Put your hosts in a config file, use keys and the agent, set up PowerShell remoting over SSH when you need structured automation, and make scripts non-interactive. That combination covers most of what Windows sysadmins and DevOps engineers actually do with SSH from PowerShell, and it keeps the late-night man page reading to a minimum.

All Articles

Related Articles

WinRM: The Tax You Pay for One-Liner Convenience

WinRM: The Tax You Pay for One-Liner Convenience

Your Script Worked Fine on Windows. Then It Met Linux.

Your Script Worked Fine on Windows. Then It Met Linux.