Automating SSH Setup for Bitbucket on Windows
Setting up SSH keys manually often involves jumping between the terminal, Windows Services, and web browsers. This PowerShell script streamlines the entire process into a single workflow.
Why use this script?
- Zero Configuration: It automatically enables and starts the Windows SSH Agent service, ensuring your keys persist across sessions.
- Secure Standards: It generates a high-security ED25519 key—the modern standard for performance and safety.
- Automatic Config: It updates your
~/.ssh/configfile so that Bitbucket always knows exactly which identity file to use without you needing to specify it in every git command. - Integrated Workflow: The script detects "Bitbucket" in your key name, copies the public key to your clipboard, and opens the Bitbucket settings page for you.
When to use it:
Use this script whenever you are setting up a new development environment or need to rotate your credentials. It eliminates manual errors like typos in the config file or forgetting to start the background agent, ensuring your first git clone just works.
# 1. Start and Configure SSH Agent
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent
Get-Service ssh-agent | Select-Object Name, Status, StartType
# 2. Create directory and generate key
$sshPath = Join-Path $HOME ".ssh"
if (-not (Test-Path $sshPath)) { mkdir $sshPath | Out-Null }
cd $sshPath
ssh-keygen -t ed25519 -b 4096 -f $provider -N '""' -q
ssh-add $provider
# 3. Add to SSH Config
$configPath = Join-Path $sshPath "config"
$configBlock = @"
Host bitbucket.org
AddKeysToAgent yes
IdentityFile ~/.ssh/$provider
"@
Add-Content -Path $configPath -Value $configBlock
# 4. Display Public Key
Write-Host "`nYour SSH public Key:" -ForegroundColor Green
Get-Content "$provider.pub"
if ($provider -like "*bitbucket*") {
# 6. Pause before testing
Write-Host "`nStep 1: Paste the key into your Bitbucket account settings." -ForegroundColor White
$url = "https://bitbucket.org/account/settings/ssh-keys/"
Write-Host "`nBitbucket detected! Opening SSH settings page..." -ForegroundColor Yellow
Start-Process $url
Read-Host "Step 2: Once added, press ENTER to test the connection"
# 7. Test the connection
Write-Host "`nTesting connection to Bitbucket..." -ForegroundColor Cyan
ssh -T git@bitbucket.org
}