How to Establish SSH Connections Directly in PowerShell Without PuTTY


2 views

Since PowerShell 5.1 and later versions (including PowerShell Core 6+), Microsoft has built-in native SSH support. This eliminates the need for third-party tools like PuTTY for basic SSH operations.

First check if SSH is available in your PowerShell:


# Check SSH client availability
Get-Command ssh

# Expected output if available:
# CommandType Name Version Source
# ----------- ---- ------- ------
# Application ssh  0.0.1.0 C:\Windows\System32\OpenSSH\ssh.exe

The simplest connection command mirrors Linux terminal syntax:


ssh username@hostname -p port

Example connecting to a Ubuntu server:


ssh admin@192.168.1.100 -p 22

PowerShell's SSH implementation supports many standard OpenSSH features:


# Using SSH config files
ssh -F ~/.ssh/config myserver

# Port forwarding example
ssh -L 8080:localhost:80 user@remotehost

# Running remote commands
ssh user@host "ls -la"

Setting up SSH keys works similarly to Linux environments:


# Generate key pair
ssh-keygen -t rsa -b 4096

# Copy public key to server
ssh-copy-id -i ~/.ssh/id_rsa.pub user@host

# Connect using key authentication
ssh -i ~/.ssh/id_rsa user@host

For long-running tasks, consider these approaches:


# Using tmux/screen via SSH
ssh user@host "tmux new -s mysession"

# Background processes
ssh user@host "nohup ./long_script.sh > output.log 2>&1 &"

For connection problems:


# Verbose output for debugging
ssh -vvv user@host

# Check known_hosts file
Test-Path ~/.ssh/known_hosts

# Reset known_hosts if needed
Remove-Item ~/.ssh/known_hosts

For advanced scenarios, the Posh-SSH module provides additional features:


# Install the module
Install-Module -Name Posh-SSH -Force -Scope CurrentUser

# Example usage
New-SSHSession -ComputerName '192.168.1.100' -Credential (Get-Credential)
Invoke-SSHCommand -Index 0 -Command 'hostname'

When connecting from Windows:


# Fix line endings for remote scripts
ssh user@host "dos2unix script.sh"

# Handling Windows paths in remote commands
ssh user@host "cmd /c 'type C:\\path\\to\\file.txt'"

Since PowerShell 5.1 and particularly in PowerShell Core (6.0+), Microsoft has integrated native SSH functionality. This eliminates the need for external tools like PuTTY. To verify your SSH capabilities:

Get-Command -Name ssh

If this returns the SSH command path, you're ready to go. If not, you'll need to enable the optional feature:

# For Windows 10/11
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0

# For PowerShell Core on any platform
# SSH is typically included by default

The fundamental command structure mirrors standard SSH usage:

ssh username@hostname -p port

For example, connecting to a Ubuntu server:

ssh admin@192.168.1.100 -p 22

While password authentication works, key-based auth is more secure. Here's how to set it up:

# Generate SSH keys
ssh-keygen -t rsa -b 4096

# Copy public key to remote server
ssh-copy-id -i ~/.ssh/id_rsa.pub user@hostname

You can integrate SSH with PowerShell remoting for more advanced scenarios:

# Create a remote session
$session = New-PSSession -HostName UbuntuServer -UserName admin

# Enter the session
Enter-PSSession $session

# Run commands directly
Invoke-Command -Session $session -ScriptBlock { Get-Process }

For programmatic SSH access within scripts, consider the SSH.NET library:

# Install the module
Install-Module -Name SSH-Sessions

# Example usage
$session = New-SshSession -ComputerName 'linuxserver' -Username 'admin'
Invoke-SshCommand -SessionId $session.SessionId -Command 'ls -la'

If you encounter connection problems:

# Verbose logging
ssh -v user@hostname

# Check known_hosts conflicts
ssh-keygen -R hostname

For Windows-specific firewall issues:

# Allow SSH through firewall
New-NetFirewallRule -Name 'SSH-In-TCP' -DisplayName 'SSH (TCP-In)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22