PowerShell - Using Git with SSH Keys on Windows 10

Search for a command to run...

No comments yet. Be the first to comment.
Recently, I've been updating a bunch of Visual Studio Azure Functions and Blazor projects to .NET 8.0 from 7.0. Most went smoothly but when I came to run the Azure Functions project locally in Visual Studio, I ran into this error: Error: There is no...

Want to track how long you spend in Microsoft Teams meetings or have a request from a user for some stats? Luckily, it's actually quite easy to get the meeting durations for any user in your tenancy. Common scenarios for needing meeting duration Ok,...

One of the most requested features on Microsoft's UserVoice forum was the Support for Dynamic '+' Email Aliases in Office 365. This has been pending since 2017 and Microsoft finally implemented it in September 2020. Woohoo! However, it's not enabled ...

My computer is in the lounge and it tends to be left on for most of the day (especially with WFH in full effect). With 4 monitors hooked up, it actually outputs a decent amount of light and if you're trying to watch a movie on the TV it can be distra...

Do you run a WordPress site? Have you configured it so that its contents are automatically backed up in case of failure/hack/human error? If it's a no, then you better get cracking as it can happen to anyone. Check out how you can automatically backu...

Using Git on Windows is pretty straight forward. You can just install Git for Windows and use either the Git GUI app or Git Bash console.
But what if you use PowerShell as your default console of choice and don't want to switch? Furthermore, what if you're connecting via SSH keys?
Well with the help of a couple of modules you can setup PowerShell to automatically import SSH keys on load.
PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
Install-Module posh-sshell -Scope CurrentUser
# NOTE: If the AllowPrerelease parameter is not recognized, update your version of PowerShellGet:
# Install-Module PowerShellGet -Scope CurrentUser -Force -AllowClobber
posh-git is a PowerShell module that integrates Git and PowerShell. It Includes:
posh-sshell is a PowerShell module that provides utilities for working with SSH connections. We're mainly using it to ensure the Ssh-Agent is running.
Now that everything is installed. You'll want a way to automatically import the modules on load. You can configure your PowerShell profile to import those modules in each new session.
notepad $profile.CurrentUserAllHosts
# $profile.CurrentUserAllHosts - is the profile used by all consoles including integrated terminals like VS Code.
# $profile.CurrentUserCurrentHost - is the profile used by the current terminal you are using.
Import-Module posh-git
Import-Module posh-sshell
Start-SshAgent

C:\Users\[Username]\.ssh\id_rsa.pub are automatically loaded. You can add more by using ssh-add command.And that's it. Now when you open a new PowerShell console, you should be able to connect to remote sources using SSH. If your key has a passphrase then you will be prompted for that only when the ssh-agent service starts.
Ensure you have installed Git for Windows and that the path to git is in your PATH environment variable.

If it's the first time you are connecting to the remote host you may need to trust it. You can run the following command to test the connection:
ssh -T [email protected]
# Replace connection with the service you are connecting to.
You may see a warning that the authenticity of the host can't be established. Type: 'yes' to accept connection. You should now be able to connect if your ssh key is valid.