How to SSH using private key on Windows

This simple command allows you to SSH to a server from Windows Powershell using a private key pem file:

ssh -i keyfile.pem

You may see the following error upon trying the SSH command from Windows Powershell:

Permissions for '.\\ElastiCourse.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key ".\\ElastiCourse.pem": bad permissions

On Linux, this is fixed with the command chmod 600 ElastiCoure.pem on the private key file, however Windows does not have an equivalent method. Instead you can utilize the icacls.exe tool to fix permissions for the private key as follows (change directory in the path variable to match the key location if in a different directory):

$path = ".\ElastiCourse.pem"
icacls.exe $path /reset
icacls.exe $path /GRANT:R "$($env:USERNAME):(R)"
icacls.exe $path /inheritance:r

Now you should be able to SSH to your server from Powershell using pem key file.