SSH - Public Key Authentication using the Posh-SSH Module with PowerShell

This article has not been completed yet. However, it may already contain helpful information and therefore it has been published at this stage.

Introduction:

Creating a Key Pair

  • Launch PuTTYgen

To create a new key pair, select the type of key to generate from the bottom of the screen.

NOTE: Using SSH-2 RSA with 2048 bit key size is good for most people.

  • Click on Generate

Now the public key has to be stored on the SSH / SFTP server. Depending on the type of server (Windows / Linux) and software (e.g. OpenSSH server / Bitvise SSH server), the subsequent procedure varies.
I have already written a blog article about OpenSSH where this is covered.

https://it-infrastructure.solutions/installing-openssh-windows/

I will not go further into detail at this point, as this is no longer in the scope of the article.

Now we will look into the authentication process on the SSH / SFTP server using the generated keys and the Posh Module from within the PowerShell.

If you want to know how to install the Posh Module, you should consult the following blog post written by me.

https://it-infrastructure.solutions/how-to-transfer-files-with-powershell-and-sftp/

$computer = '127.0.0.1'
$username = 'test'
$password = 'test' | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $password
$keyfile  = 'C:\Users\Thomas\OneDrive - IVB Immobilienverwaltung & -vermittlung Bründl GmbH\Scripts\COFAG\key.priv'

# New-SSHSession -ComputerName $computer -Credential $credential
New-SSHSession -Computer $computer -Credential $credential -Keyfile $keyfile


try {
# Removing Session
Remove-SSHSession -SessionId ((Get-SSHSession).SessionId)  -Verbose
}
catch {
$_.exception.Message
}



# New-SFTPSession -ComputerName $computer -Credential $credential
New-SFTPSession -Computer $computer -Credential $credential -Keyfile $keyfile


try {
# Removing Session
Remove-SFTPSession -SessionId ((Get-SFTPSession).SessionId)  -Verbose
}
catch {
$_.exception.Message
}

PuTTYgen Download
PuTTYGen is a key generator software for Putty Application.
How to connect to linux server using ssh with private key from PowerShell?
I have generated an ssh key pair using puttygen and can successfully connect to my CentOS server with my private key from putty on my Windows 10 workstation: I want to connect to the server from W...