Run ssh command directly in windows Powershell

Devashish Patil
CodeByte
Published in
2 min readSep 24, 2020
Image source: unsplash.com

For all those who want to work with remote Linux servers from their Windows computer, there are several ways to connect. For example, you might be using PuTTy or MobaXterm already. These tools are great and have some additional features as well.

What if you just want to use PowerShell for your ssh command. Yes, windows now have support for ssh in PowerShell. That’s not enough though, just like you use chmod 400 <your .pem file> for the permissions on Linux, you need to do a similar thing on Windows as well. So, I have noted down a list of PowerShell commands to do this.

$path = "Path\to\your\ssh_key.pem"#Get current ACL to file/folder
$acl = Get-Acl $path
#Disable inheritance and remove inherited permissions
$acl.SetAccessRuleProtection($true,$false)
#Remove all explict ACEs
$acl.Access | ForEach-Object { $acl.RemoveAccessRule($_) }
#Create ACE for owner with read-access. You can replace $acl.Owner with $env:UserName to give permission to current user
$ace = New-Object System.Security.AccessControl.FileSystemAccessRule -ArgumentList $acl.Owner, "Read", "Allow"$acl.AddAccessRule($ace)
#Save ACL to file/folderSet-Acl -Path $path -AclObject $acl

Just update the path to your .pem key file and run these commands, you’ll be good to go. To ssh into your remote server, just run:

ssh -i "Path\to\your\ssh_key.pem" user@hostname

Hope, this helps.

--

--