PowerShell script for installing Git for Windows and basic Git + SSH setup for a selected Windows user.
The script is designed to run on a Windows server from an elevated PowerShell session. It:
- installs Git via
wingetor a local installer; - configures Git in a dedicated user
.gitconfig; - accepts the SSH private key as a parameter;
- copies the private key to
%USERPROFILE%\.ssh\id_ed25519; - auto-generates
id_ed25519.pubwhen possible; - creates or updates
%USERPROFILE%\.ssh\config; - asks for the Git server host name at runtime if
-GitServerHostis not provided; - writes a managed
Hostblock for the target Git server; - removes an old
core.sshCommandfrom the user.gitconfig, so Git uses the standard user OpenSSH config; - locks down ACLs for
.ssh, keys,config,known_hosts, and.gitconfig.
By default the script sets:
user.nameuser.emailinit.defaultBranch=mainfetch.prune=truepull.rebase=trueorfalsepush.default=simplerebase.autoStash=truecore.autocrlf=falsecredential.helper=manager-coreunless disabled
It also creates a managed block in %USERPROFILE%\.ssh\config, for example:
Host gitlab.example.com
HostName gitlab.example.com
User git
Port 22
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
PreferredAuthentications publickey-Username-GitUserName-GitUserEmail- one of:
-SshPrivateKeyPath-SshPrivateKey
-SshPublicKeyPath-SshPublicKey-GitServerHost- if omitted, the script asks for it interactively
-GitServerSshUser(default:git)-GitHostAlias- if omitted, it defaults to
GitServerHost
- if omitted, it defaults to
-GitServerPort(default:22)-InstallMode auto|winget|local|skip-LocalInstallerPath-GitPackageId(default:Git.Git)-DefaultBranch(default:main)-PullMode rebase|merge(default:rebase)-AutoCrlf false|true|input(default:false)-EnableCredentialManager-InstallGitLfs-Force- allows overwriting existing
id_ed25519/id_ed25519.pubwhen contents differ
- allows overwriting existing
Set-ExecutionPolicy Bypass -Scope Process -Force
.\setup-windows-git-basic.ps1 `
-Username user-git `
-GitUserName "Alexander" `
-GitUserEmail "alex@example.com" `
-SshPrivateKeyPath C:\user-git\id_ed25519 `
-InstallMode auto.\setup-windows-git-basic.ps1 `
-Username user-git `
-GitUserName "Alexander" `
-GitUserEmail "alex@example.com" `
-SshPrivateKeyPath C:\user-git\id_ed25519 `
-GitServerHost github.com `
-GitServerSshUser git.\setup-windows-git-basic.ps1 `
-Username user-git `
-GitUserName "Alexander" `
-GitUserEmail "alex@example.com" `
-SshPrivateKeyPath C:\user-git\id_ed25519 `
-GitServerHost github.com `
-GitHostAlias my-git$priv = Get-Content C:\user-git\id_ed25519 -Raw
.\setup-windows-git-basic.ps1 `
-Username user-git `
-GitUserName "Alexander" `
-GitUserEmail "alex@example.com" `
-SshPrivateKey $privgit --version
git config --file "C:\Users\user-git\.gitconfig" --list
type C:\Users\user-git\.ssh\config
type C:\Users\user-git\.ssh\id_ed25519.pubSSH connectivity check:
ssh -F "C:\Users\user-git\.ssh\config" -T github.comThe target Windows user must already exist. It is better if the user has logged in at least once so the profile already exists.
-SshPrivateKeyPath / -SshPrivateKey is the private SSH key used by Git over SSH.
The script can still save the key, but automatic public-key generation may fail without interactive passphrase entry.
The script creates an empty known_hosts, but does not pre-load server fingerprints.
The first SSH connection may ask you to confirm the server host key.
The script does not overwrite the whole SSH config. It only creates or updates a marked block:
# BEGIN managed by setup-windows-git-basic.ps1 : <alias>
...
# END managed by setup-windows-git-basic.ps1 : <alias>