DEV Community

Rafi Bagaskara Halilintar
Rafi Bagaskara Halilintar

Posted on Originally published at termique.app

ssh-copy-id on Windows: The PowerShell Equivalent (2026)

You generated a key with ssh-keygen in PowerShell, typed ssh-copy-id user@server like every Linux tutorial says, and got "The term 'ssh-copy-id' is not recognized". That is expected. Windows ships ssh.exe, scp, sftp, ssh-keygen and ssh-agent, but not ssh-copy-id, which is a shell script that comes with OpenSSH on Unix-like systems.

This guide gives you a ssh-copy-id equivalent for Windows that behaves like the real thing: it creates ~/.ssh with the right permissions, does not add the same key twice, and does not glue your key onto the end of someone else's line. We tested the server side against OpenSSH 9.6 on Ubuntu 24.04, including three ways the popular one-liner quietly breaks.

Quick Summary: What You'll Learn

  • A single PowerShell command that copies your public key to a Linux server, safe to run more than once.
  • Why the common type ... | ssh "cat >> .ssh/authorized_keys" line fails on fresh servers and can break existing keys.
  • How to use the real ssh-copy-id from WSL, including with a key that lives in your Windows profile.
  • Where the key goes when the server is Windows, including the separate file for administrators.
  • What to check when the key is copied but ssh still asks for a password.

What is the ssh-copy-id equivalent on Windows?

Pipe your public key into ssh and let a small shell script on the server append it. From PowerShell, with an Ed25519 key in the default location:

Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub" | ssh user@server 'umask 077; mkdir -p ~/.ssh; f=~/.ssh/authorized_keys; touch $f; t=$(mktemp); tr -d ''\r'' > $t; [ -s $f ] && [ $(tail -c1 $f | wc -l) -eq 0 ] && echo >> $f; grep -qxFf $t $f || cat $t >> $f; rm -f $t'
Enter fullscreen mode Exit fullscreen mode

You type the server password once. After that, test that the key works on its own:

ssh -o PreferredAuthentications=publickey user@server
Enter fullscreen mode Exit fullscreen mode

If that logs you in without a password prompt, you are done. The rest of this article explains each piece, the variants for WSL and Windows servers, and what to check when it does not work.

Two notes before you run it. Connect once with plain ssh user@server first so you can check and accept the host key in a normal interactive session (our host key guide covers what that prompt means). And the server account's login shell needs to be a POSIX-style shell such as sh, bash or zsh, because sshd runs the remote command through that shell.

Why doesn't Windows have ssh-copy-id?

ssh-copy-id is not a compiled program. It is a shell script distributed with portable OpenSSH, and on Debian and Ubuntu it is part of the openssh-client package. Its man page describes it as a script that "uses ssh(1) to log into a remote machine", checks which of your keys are already accepted, and appends the rest to ~/.ssh/authorized_keys, creating the file and directory if needed.

The Win32 port of OpenSSH that Microsoft ships for Windows 10 (build 1809 and later), Windows 11 and Windows Server includes the client binaries but not that script. Microsoft's own key management docs suggest copying the public key with scp or writing it with PowerShell instead. You can check which OpenSSH pieces you have with:

Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Enter fullscreen mode Exit fullscreen mode

If OpenSSH.Client is not installed, Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0 adds it from an elevated prompt.

What does the common one-liner get wrong?

The version you find in most answers is this:

type $env:USERPROFILE\.ssh\id_ed25519.pub | ssh user@server "cat >> .ssh/authorized_keys"
Enter fullscreen mode Exit fullscreen mode

It works on a server where ~/.ssh/authorized_keys already exists and ends with a newline. Outside that case, we hit three problems in testing.

It fails on a fresh account

If ~/.ssh does not exist yet, which is normal for a new user or a fresh VM without cloud keys, the redirect has nowhere to write. With sh (dash on Ubuntu) as the remote shell you get cannot create .ssh/authorized_keys: Directory nonexistent; with bash you get .ssh/authorized_keys: No such file or directory. Nothing is copied.

It can merge two keys into one line

>> appends bytes. It does not check whether the file ends with a newline. Files edited by hand, pasted into a web console, or written by some tools often do not. We reproduced this on a test sshd: an authorized_keys file containing key A with no trailing newline, then key B appended with cat >>.

The file now had one line. Key A still logged in, because sshd reads the first two fields as the key and treats everything after as a comment. Key B was refused:

Permission denied (publickey).
Enter fullscreen mode Exit fullscreen mode

The server log showed Failed publickey for root from 127.0.0.1 ... ED25519 SHA256:..., with no hint that the key was sitting right there, glued to the previous line. That is a confusing failure to debug from the client side.

It adds the same key again on every run

Run it twice and you get two copies. That is harmless for authentication, but it makes the file harder to audit and it makes removing a key later error-prone. ssh-copy-id avoids this by testing which keys already work before it copies anything.

There is also a quieter detail. When PowerShell pipes text into a native program like ssh.exe, each line arrives with a Windows line ending. In our test sshd accepted a key line ending in \r\n, with and without a comment, so logins work. But a duplicate check with grep -x did not match the CRLF line against the same key with a Unix line ending. If you add a dedupe step, strip the carriage return first.

How do you copy an SSH key to a Linux server from PowerShell?

Here is the full flow, assuming the OpenSSH client is installed.

  • Generate a key if you do not have one. ssh-keygen -t ed25519 writes id_ed25519 and id_ed25519.pub to C:\Users\<you>\.ssh\. Set a passphrase; our ssh-keygen best practices explain the choices.
  • Connect once with ssh user@server to verify the host key and confirm the password works. Type exit.
  • Run the one-liner from the first section. Change the file name if your key is not id_ed25519.pub.
  • Test with ssh -o PreferredAuthentications=publickey user@server.
  • Only after a key login works, consider turning off password authentication on the server, and keep a second session open while you test that change.

What each part of the command does

The PowerShell side reads the .pub file and pipes it into ssh. Everything inside the single quotes runs on the server. Written out as a normal script it looks like this:

umask 077                          # new files 600, new dirs 700
mkdir -p ~/.ssh
f=~/.ssh/authorized_keys
touch $f
t=$(mktemp)
tr -d '\r' > $t                    # save the piped key, drop Windows CRs
[ -s $f ] && [ $(tail -c1 $f | wc -l) -eq 0 ] && echo >> $f   # add a missing final newline
grep -qxFf $t $f || cat $t >> $f   # append only if the exact line is not there
rm -f $t
Enter fullscreen mode Exit fullscreen mode

We ran this script on Ubuntu 24.04 against the cases above: a home directory with no ~/.ssh (it created ~/.ssh as 700 and authorized_keys as 600), a file with no trailing newline (the new key landed on its own line), and the same key sent twice with CRLF endings (one copy, no carriage returns left in the file).

umask 077 only affects files and directories the script creates. If ~/.ssh already exists with loose permissions, it leaves them alone; see the troubleshooting section below.

Why single quotes, and why ''\r''?

PowerShell does not expand anything inside single quotes, so $f, $(mktemp) and ~ reach the server untouched. A literal single quote inside a single-quoted PowerShell string is written as two single quotes, which is why the tr argument appears as ''\r''. The server receives tr -d '\r'.

The command avoids double quotes on purpose. How PowerShell passes embedded double quotes to native programs changed in PowerShell 7.3 (the $PSNativeCommandArgumentPassing setting), and Windows PowerShell 5.1 still uses the legacy behavior. Keeping the remote script free of double quotes means it reaches ssh.exe the same way in both.

Can you use the real ssh-copy-id from WSL?

Yes, and if WSL is already installed it is the least typing. Ubuntu and Debian in WSL ship ssh-copy-id as part of openssh-client.

For a key you created inside WSL:

ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
Enter fullscreen mode Exit fullscreen mode

For a key you created in Windows, point at the file through /mnt/c:

ssh-copy-id -f -i /mnt/c/Users/<you>/.ssh/id_ed25519.pub user@server
Enter fullscreen mode Exit fullscreen mode

The -f flag is forced mode. The man page says it "doesn't check if the keys are present on the remote server" and therefore "does not need the private key". That matters here because files under /mnt/c often show up with permissions that OpenSSH refuses to use for a private key, so the check ssh-copy-id normally does would fail anyway. The tradeoff is the one the man page names: forced mode can install a second copy if the key is already there.

Two other flags are worth knowing. -n is a dry run that prints which keys would be installed. -s switches to SFTP mode, which downloads authorized_keys, edits it locally and uploads it again; use it when the server restricts which commands you can run. -p and -o pass a port and other ssh options through, though a Host block in ~/.ssh/config is usually tidier.

Git Bash is another option. Check with command -v ssh-copy-id inside Git Bash; if it prints a path, the same commands work there. If it prints nothing, use the PowerShell method.

How do you copy an SSH key to a Windows server?

If the server runs Microsoft's OpenSSH Server, the target file depends on the account:

  • Standard user: C:\Users\<username>\.ssh\authorized_keys
  • Member of the local Administrators group: C:\ProgramData\ssh\administrators_authorized_keys, and the per-user file is not used. The file's ACL must allow only Administrators and SYSTEM.

Microsoft's documentation builds a remote PowerShell command for this. For a standard user:

$authorizedKey = Get-Content -Path $env:USERPROFILE\.ssh\id_ed25519.pub
$remotePowershell = "powershell New-Item -Force -ItemType Directory -Path $env:USERPROFILE\.ssh; Add-Content -Force -Path $env:USERPROFILE\.ssh\authorized_keys -Value '$authorizedKey'"
ssh username@hostname $remotePowershell
Enter fullscreen mode Exit fullscreen mode

For an administrator, the same pattern writes to $env:ProgramData\ssh\administrators_authorized_keys and then runs icacls.exe with /inheritance:r /grant "Administrators:F" /grant "SYSTEM:F". Microsoft's page has the exact quoting, and a variant using the SID *S-1-5-32-544 for non-English systems where the group is not called "Administrators". Copy it from there rather than retyping it.

Note that $env:USERPROFILE in those commands is expanded on your machine before the command is sent, so it only produces the right remote path when your local and remote profile paths match. Write the remote path out in full if they differ. We did not test the Windows server side ourselves; the commands above are quoted from Microsoft's docs (Ed25519 file name substituted).

Why does SSH still ask for a password after copying the key?

Work through these in order. ssh -v user@server on the client and the sshd log on the server (journalctl -u ssh on Ubuntu) tell you which one you hit.

  • Permissions. With the default StrictModes yes, sshd ignores an authorized_keys file other users can write to. In our test a mode 666 file produced Authentication refused: bad ownership or modes for file ... in the server log, and the client just saw Permission denied (publickey). Fix with chmod 700 ~/.ssh and chmod 600 ~/.ssh/authorized_keys, owned by the user.
  • Two keys on one line. Open the file and check that every key starts on its own line. This is the trailing-newline problem from earlier.
  • Wrong encoding. If you built authorized_keys on Windows and uploaded it with scp, check how it was written. In Windows PowerShell 5.1, > and Out-File write UTF-16LE, and Set-Content -Encoding UTF8 writes a byte-order mark. Our test sshd refused the key in both cases. PowerShell 7 defaults to UTF-8 without a BOM. file authorized_keys on the server shows the encoding.
  • The client offers a different key. If you have several keys or an agent full of them, the server may give up before your new key is tried. Point at it with ssh -i ~/.ssh/id_ed25519 -o IdentitiesOnly=yes user@server, or set IdentityFile in a Host block. Our guide to managing multiple SSH keys across devices covers this.
  • Wrong account. The key went to the home directory of the user you logged in as. ssh deploy@server and ssh ubuntu@server read different files.
  • Key in PuTTY format. OpenSSH on the server cannot use a .ppk public key export. See converting PPK to OpenSSH.

Where does termique fit?

If you do this once, the PowerShell line is fine. If you do it for every new server, it is one more thing to paste correctly each time.

termique, the SSH manager we build, has this step built in. Open a session to the server with its current password, select a key under SSH keys, and choose Install on host. termique creates ~/.ssh and authorized_keys with the right permissions if they are missing and adds the key only if it is not already there, which is the same behavior the script above reproduces by hand. Then switch the host's authentication to the stored key.

You can generate the key in termique too (Ed25519, ECDSA or RSA), or import an existing OpenSSH key, including passphrase-protected ones. Private keys are encrypted on your device before they sync, so the sync server only stores ciphertext; hosts, groups and snippets sync as plaintext metadata. The full model is on our security page. The wider set of habits around keys and access is in the SSH security best practices guide.

FAQ

Is there an ssh-copy-id for Windows 11?

Not as a built-in command. Windows 11 includes the OpenSSH client (ssh, scp, sftp, ssh-keygen), but not the ssh-copy-id script. Use the PowerShell one-liner in this guide, or run the real ssh-copy-id from WSL.

Why does PowerShell say 'ssh-copy-id' is not recognized?

Because it does not exist on Windows. The error is PowerShell telling you there is no command, script or program with that name on your PATH. It is not a sign that OpenSSH is broken.

Can I just paste the key into authorized_keys by hand?

Yes. Copy the single line from your .pub file, open ~/.ssh/authorized_keys on the server in an editor, and paste it on a new line. Make sure the line was not wrapped during the paste, and set the permissions to 600 on the file and 700 on the directory.

Does ssh-copy-id copy my private key?

No. It only installs public keys. In forced mode (-f) it does not even need the private key to be present. Your private key should never leave the machines you log in from.

Where is authorized_keys for an admin user on Windows Server?

In C:\ProgramData\ssh\administrators_authorized_keys, not in the user's profile, and its ACL must allow only Administrators and SYSTEM. This applies to accounts in the local Administrators group.

If you add keys to new servers every week, try termique free: generate a key, install it on a host from an open session, and keep the private key encrypted across your devices.

Top comments (0)