How do I use SSH key paths that contain spaces on Windows?
Quote the entire key-file path so that spaces remain part of one argument. CMD and PowerShell expand environment variables differently, even though the SSH options themselves are the same.
Instructions
1. Verify the exact file in PowerShell
The identity file is the private key without the .pub suffix. Check its existence without printing its contents.
Test-Path -LiteralPath "$env:USERPROFILE\.ssh\server key"True confirms a file or directory exists at the literal path; confirm that your intended key is a file.
2. Connect using the correct shell syntax
Replace the sample filename with your existing private key. Do not copy a key from an unknown source.
ssh -o IdentitiesOnly=yes -i "$env:USERPROFILE\.ssh\server key" adminuser@203.0.113.10PowerShell expands $env:USERPROFILE inside double quotes.
ssh -o IdentitiesOnly=yes -i "%USERPROFILE%\.ssh\server key" adminuser@203.0.113.10CMD expands %USERPROFILE%. The quotes keep the complete path together.
3. Keep remote arguments separate
A local key path is interpreted by Windows. A path following a remote file-transfer destination is interpreted in the server context. If nested quoting becomes complex, connect interactively and run the remote command there.
Check the result
- SSH does not report that the identity file is inaccessible.
- The login uses the intended account and key.
Troubleshooting
- PowerShell treats %USERPROFILE% as text
- Use its $env:USERPROFILE syntax instead.
- Permission denied still appears
- A valid local path does not mean the server accepts that public key for the selected account. Check authentication separately.
Official references
Use the documentation for your installed version when a flag, service name or platform feature differs.
